tableViewCell 中的 UIAlertController - Swift

UIAlertController in tableViewCell - Swift

我在 tableviewcell 的每个单元格中都有一个按钮。单击按钮时,在单元格编码页面中,我有一个已经在运行的操作。但是我决定让用户在动作开始之前确认它,所以在动作中添加了一个 UIAlertController 。警报控制器在 UIviewcontroller 中工作正常,但在 UItableviewcell 中使用下面的代码我收到错误消息:"does not have a member named presentviewcontroller"。我怎样才能让它发挥作用?我的代码在这里。谢谢

@IBAction func followBtn_click(sender: AnyObject) {



        if title == "Following" {

            var refreshAlert = UIAlertController(title: "Sure?", message: "Do you want to unfollow this person?", preferredStyle: UIAlertControllerStyle.Alert)
            refreshAlert.addAction(UIAlertAction(title: "Yes", style: .Default, handler: { (action: UIAlertAction!) in

            var query = PFQuery(className: "follow")

            query.whereKey("user", equalTo: PFUser.currentUser()!.username!)
            query.whereKey("userToFollow", equalTo: usernameLbl.text!)

            var objects = query.findObjects()

            for object in objects! {

                object.delete()

            }

            }))

            refreshAlert.addAction(UIAlertAction(title: "No", style: .Default, handler: { (action: UIAlertAction!) in
                println("Cancel unfollow")
            }))

            self.presentViewController(refreshAlert, animated: true, completion: nil)

         /*  self.window?.rootViewController?.presentViewController(refreshAlert, animated: true, completion: nil)

            when I use the above to make it run, the code runs but nothing happens and I get the warning below in the output area

             2015-07-02 08:57:22.978 MyLast[968:200872] Warning: Attempt to present <UIAlertController: 0x7f849c04ed20> on <UINavigationController: 0x7f8499c69020> whose view is not in the window hierarchy!



          */



        }

        else {

            println(“rest will work”)
           /*   rest of the code */

        }


}

确保您的表格视图控制器位于导航控制器的层次结构中或嵌入在 UINavigationController 中。因为 UIAlertView 在 swift 中被弃用并且有一个 UIAlertViewController 并且无论何时你需要或呈现警报你都必须有一个控制器层次结构。

您也可以使用相同的代码显示 ActionSheet,只需稍作更改 UIAlertControllerStyle is action sheet。

另一个建议是尝试将处理程序代码删除为 nil

alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))