Reputation: 489
I have menuTablecontroller in which I took customcell with two labels and another Inner tableviewcontroller . Now here in Innertableview I have ItemCustomcell with two labels and a button. I have to add Alertviewcontroller on clicking this button. I have tried by creating protocols into the menuTabletablecontroller set the delegate but its not working ..
My menutableviewcontroller
protocol customPostcodeAlertDelegate {
func showAlert(title:String,message:String)
}
class MenuTableViewController: UITableViewController {
func showAlert(title:String,message:String){
let alert = UIAlertController(title: "Choose Mode of Order", message:"", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
.....
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("subcatcell", forIndexPath: indexPath)as! SubcatTableViewCell
return cell
}
Now in SubcatTableViewCell i have a button on its custom cell named itemtableviewcell
My SubcatTableViewCell
class SubcatTableViewCell: UITableViewCell,UITableViewDelegate,UITableViewDataSource {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
InnerTableView.delegate = self
InnerTableView.dataSource = self
InnerTableView.bounces = false
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("itemCell")as! ItemTableViewCell
return cell
}
Now I set the delegate in itemtableviewcell like this
class ItemTableViewCell: UITableViewCell {
var delegate: customPostcodeAlertDelegate?
@IBOutlet var itemName: UILabel!
@IBOutlet var itemPrice: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@IBAction func OrderBtnPressed(sender: AnyObject) {
self.delegate?.showAlert("Choose Mode of Order", message: "")
}
}
But after clicking orderpressed button the alertview controller not working.. where I am doing mistake or give me another idea to add the alertviewcontroller in custom tableview ???
Upvotes: 0
Views: 1280
Reputation: 751
You forgot to actually set the delegate:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("itemCell") as! ItemTableViewCell
cell.delegate = // Your MenuTableViewController
return cell
}
EDIT WITH NOTIFICATIONS
class MenuTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.showAlert(_:)), name: "showError", object: nil)
}
func showAlert(sender: NSNotification) {
let title = sender.object!["title"]
let message = sender.object!["message"]
let alert = UIAlertController(title: "Choose Mode of Order", message:"", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
....
}
class ItemTableViewCell: UITableViewCell {
@IBAction func OrderBtnPressed(sender: AnyObject) {
NSNotificationCenter.defaultCenter().postNotificationName("showError", object: [ "message" : "A message", "title" : "A Title" ])
}
}
Upvotes: 2
Reputation: 2693
You are not conforming MenuTableViewController to your protocol. You will have to conform it like MenuTableViewController: UITableViewController, customPostcodeAlertDelegate
Then you can override its method showAlert
Upvotes: 0