Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import UIKit
class NoticeCustomViewCell: UITableViewCell
{
private var myButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
}
override init(style: UITableViewCellStyle, reuseIdentifier: String!)
{
super.init(style: style, reuseIdentifier: reuseIdentifier)
myButton = UIButton()
myButton.frame = CGRectMake(20,5,250,30)
myButton.backgroundColor = UIColor.blueColor()
myButton.setTitle("Push Notice", forState: UIControlState.Normal)
myButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
myButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Highlighted)
myButton.layer.cornerRadius = 5
myButton.addTarget(self, action: #selector(NoticeCustomViewCell.onClickMyButton(_:)), forControlEvents: .TouchUpInside)
self.addSubview(myButton)
}
func onClickMyButton(sender: UIButton) {
let message = "SUCCESS"
NSNotificationCenter.defaultCenter().postNotificationName("callBack",object: message, userInfo: ["test": 999])
}
required init?(coder aDecoder: NSCoder) {
fatalError("error")
}
}
class NoticeTableSandBox: UIViewController, UITableViewDelegate, UITableViewDataSource {
private var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView = UITableView(frame: CGRect(x: 0, y:0, width: self.view.frame.width, height:self.view.frame.height))
tableView.dataSource = self
tableView.delegate = self
tableView.registerClass(NoticeCustomViewCell.self, forCellReuseIdentifier: "customCell")
self.view.addSubview(tableView)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! NoticeCustomViewCell
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
return cell
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(NoticeTableSandBox.callBack(_:)), name: "callBack", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(NoticeTableSandBox.callBackError(_:)), name: "callBackError", object: nil)
}
override func viewWillDisappear(animated: Bool) {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func callBack(notification: NSNotification) {
dispatch_async(dispatch_get_main_queue(), {
let myObject = notification.object
print(myObject)
if let userInfo = notification.userInfo {
let value = userInfo["test"]! as! Int
print(value)
}
})
}
func callBackError(notification: NSNotification) {
dispatch_async(dispatch_get_main_queue(), {
let myAlert = UIAlertController(title: "FAILED", message: "Error message", preferredStyle: .Alert)
self.presentViewController(myAlert, animated: true, completion: nil)
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.