使用 swift 代码在按钮单击中的自定义 UITableViewCell 中添加 UIAlertController?

Add UIAlertController in a custom UITableViewCell in button click using swift code?

我有 menuTablecontroller ,我在其中使用了带有两个标签的 customcell 和另一个 Inner tableviewcontroller 。现在在 Innertableview 中,我有带有两个标签和一个按钮的 ItemCustomcell。我必须在单击此按钮时添加 Alertviewcontroller。 我尝试通过在 menuTabletablecontroller 中创建协议来设置委托,但它不起作用..

我的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
  }

现在在 SubcatTableViewCell 中,我在其自定义单元格上有一个名为 itemtableviewcell 的按钮

我的 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 
 }

现在我像这样在 itemtableviewcell 中设置委托

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: "")


}


 }

但是在点击 orderpressed 按钮后,alertview 控制器不工作.. 我在哪里做错了或者给我另一个想法在自定义 tableview 中添加 alertviewcontroller ???

您没有使 MenuTableViewController 符合您的协议。你必须像 MenuTableViewController: UITableViewController, customPostcodeAlertDelegate

一样遵守它

然后你可以覆盖它的方法showAlert

您忘记实际设置委托:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("itemCell") as! ItemTableViewCell
    cell.delegate = // Your MenuTableViewController
    return cell 
 }

编辑通知

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" ])
    }
}