操作表在iPhone模拟器中不起作用

时间:2017-03-23 00:36:48

标签: iphone xcode swift3 ios10 uialertcontroller

我正在尝试在Swift中实现Action表。下面是实现它的代码。当我执行代码时,Xcode不会显示任何错误,并且操作表不会出现在模拟器中。我们非常感谢您解决问题的任何帮助。

覆盖func viewWillAppear(_ animated:Bool){         super.viewWillAppear(动画)

    func showActionSheet(sender: AnyObject) {

    let actionSheetController: UIAlertController = UIAlertController(title: "Action Sheet", message: "Choose an option!", preferredStyle: .actionSheet)

    //Create and add the Cancel action
    let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in
        //Just dismiss the action sheet
    }
    actionSheetController.addAction(cancelAction)

    self.present(actionSheetController, animated: true, completion: nil)

1 个答案:

答案 0 :(得分:1)

提供代码的方式,看起来好像是在viewWillAppear中添加了一个方法/函数。如果这不是打字错误而你确实设置了这样的代码,那么将代码修改为这样就可以使它工作:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    let actionSheetController: UIAlertController = UIAlertController(title: "Action Sheet", message: "Choose an option!", preferredStyle: .actionSheet)

    //Create and add the Cancel action
    let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in
        //Just dismiss the action sheet
    }
    actionSheetController.addAction(cancelAction)

    self.present(actionSheetController, animated: true, completion: nil)
}

基本上,viewWillAppear内没有方法,但也将您的操作表显示代码移至viewDidAppear而不是viewWillAppear