tableViewCell中的UIAlertController - Swift

时间:2015-07-02 06:05:25

标签: ios swift uialertcontroller tableviewcell

我在tableviewcell的每个单元格中都有一个按钮。单击按钮时,在单元格编码页面中,我有一个已经正常工作的操作。但是我决定让用户在操作开始之前确认它,因此在操作中添加了UIAlertController。警报控制器在UIviewcontroller中工作正常,但在UItableviewcell中,代码如下,我收到错误消息:“没有名为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 */

        }


}

1 个答案:

答案 0 :(得分:3)

确保您的tableview控制器位于导航控制器的层次结构中或嵌入UINavigationController中。因为UIAlertView在swift中已弃用而且有一个UIAlertViewController,无论何时需要或出现Alert,你都必须拥有一个Controller Hierarchy。

您也可以使用相同的代码显示ActionSheet,只需稍加更改UIAlertControllerStyle就是操作表。

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

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