【问题标题】:How do I open a alertcontroller from a button in tableview cell in ios 9?如何从 ios 9 的 tableview 单元格中的按钮打开警报控制器?
【发布时间】:2016-01-07 00:13:54
【问题描述】:

我想在表格视图单元格中打开一个带有呼叫按钮和取消按钮的警报视图。

var arrayOfUsernames: [PFObject] = [ ]

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arrayOfUsers.count
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int{
    return 1
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "Tutors"
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell: CustomTableViewCell = TutorTable.dequeueReusableCellWithIdentifier("Tutor") as! CustomTableViewCell

    cell.CUsername.text = self.arrayOfUsers[indexPath.row]
    cell.Classes.text = self.arrayOfClasses[indexPath.row]
    return cell

}

这是自定义表格视图单元

import UIKit

class CustomTableViewCell: UITableViewCell {

    @IBOutlet weak var CUsername: UILabel!
    @IBOutlet weak var Distance: UILabel!
    @IBOutlet weak var Classes: UILabel!
    @IBOutlet weak var Rating: UIImageView!

    @IBAction func bookNow(sender: AnyObject) {
    }

    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
    }

}

【问题讨论】:

    标签: uitableview swift2 ios9 cell uialertcontroller


    【解决方案1】:

    由于 UITableViewCell 是 UIView 子类,而不是 UIViewController,因此您需要创建一个变量来保存您可以从中呈现的 viewController。

    首先,您想将weak viewController 变量添加到您的CustomTableViewCell。我建议使用navigationController,因为如果您需要它来处理其他事情,您可以将它用于pushViewController:,并且如果需要,您可以从中控制堆栈。如果没有,您可以使用 UITableViewController。

    在您的cellForRowAtIndexPath: 方法中,只需将单元类中的navigationController 变量设置为当前的navigationController 或tableViewController:

    cell.navigationController = self.navigationController 或者... cell.viewController = self

    一旦你这样做了,在你的 bookNow: 方法中,你现在可以使用 UIViewController 来呈现视图。下面是代码的样子...

    你的 UITableViewController 类:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) → UITableViewCell? {
      let cell = tableView.dequeueReusableCellWithIdentifier("Tutor") as! CustomTableViewCell
      // setup your cell here
      cell.navigationController = self.navigationController
      // cell.viewController = self
      return cell
    }
    

    在您的 CustomCellClass 中:

    weak var navigationController: UINavigationController?
    // weak var viewController: UIViewController?
    
    @IBAction func bookNow(sender: AnyObject) {
      let alertController = UIAlertController(/* setup */)
      // setup alertController
      self.navigationController?.presentViewController(alertController, animated: false, completion: nil)
      // self.viewController?.presentViewController(alertController, animated: false, completion: nil)
    }
    

    附带说明,我强烈建议您将变量名称更改为小写。大写通常用于Class 名称

    【讨论】:

    • 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[ setValue:forUndefinedKey:]:此类不符合键 bookNow 的键值编码。”这是我得到的错误^
    • 当我使用你告诉我的导航控制器方式时
    • 没关系,非常感谢它,我遇到了一个 segue 问题
    • 如果有效,您应该接受它作为答案
    【解决方案2】:

    @meteochu 建议的方式可行,但我更喜欢协议委托模式。

    请记住,按钮位于 tableView 单元格中,您可以使用委托,以便您的 ViewController 进行警报的呈现。这是CustomtableViewCell 中的协议:

    protocol CustomTableViewCellDelegate: class {
       func customTableViewCell(customTableViewCell: cell, didTapButton button: UIButton)
    }
    

    然后在您的CustomTableViewCell 中添加一个属性weak var delegate: CustomTableViewCellDelegate?

    之后,再次在您的CustomTableViewCell 中,在您的bookNow 操作中,您可以像这样解雇代表:

    self.delegate.customTableViewCell(self, didTapButton: sender as! UIButton) // Or just 'sender', if you set your action type to UIButton
    

    最后,在 TableView 所在的 ViewController 中,确保通过实现其协议订阅 CustomTableViewCell 的委托:

    // Note the CustomTableViewCellDelegate here
    class ViewController: CustomTableViewCellDelegate, UITableViewDelegate, UITableViewDataSource {
       // ...
       func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
          let cell: CustomTableViewCell = TutorTable.dequeueReusableCellWithIdentifier("Tutor") as! CustomTableViewCell
          // ...
          cell.delegate = self
          return cell
       }
    
       // Implementing the protocol function
       func customTableViewCell(customTableViewCell: cell, didTapButton button: UIButton) {
    
          let phone = "+888888888888" // Sample phone number
    
          // Alert code
          let alertController = UIAlertController(title: "Call \(phone)", message:
            "Are you sure you want to perform this call?", preferredStyle: .Alert)
    
          // Call Button
          alertController.addAction(UIAlertAction(title: "Call", style: .Destructive) { action in
          // Code for making a call
          if let url = NSURL(string: "tel://\(phone)") {
              UIApplication.sharedApplication().openURL(url)
          } else
          })
    
          alertController.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))  // Cancel button
          self.presentViewController(alertController, animated: true, completion: nil)  // Show the alert on the screen, here the ViewController does the presenting
       }
    }
    

    如您所见,要创建警报,您需要使用UIAlertController,并以UIAlertControllerStyle.Alert 的样式调用它,并使用正确的标题和消息。

    然后,您只需使用.addAction 添加操作(按钮)并在其完成处理程序中键入您要执行的代码。最后,您需要记住使用presentViewController 显示您的警报。

    如果您的号码发生变化,协议会引用单元格,您可以使用它来传递单元格属性。

    查看UIAlertViewController类的官方文档。

    【讨论】:

    • 当我从 tableviewcell 继承 uialertcontroller 时存在多重继承问题
    • 感谢一半,另一部分由其他人回答
    • URL 方案电话没有注册处理程序?我如何为此注册一个 url 方案?
    • 我已经编辑了我的答案以反映@meteochu 所说的内容
    猜你喜欢
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多