【问题标题】:UIAlertController in tableViewCell - SwifttableViewCell 中的 UIAlertController - Swift
【发布时间】:2015-07-02 06:05:24
【问题描述】:

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 */

        }


}

【问题讨论】:

    标签: ios swift uialertcontroller tableviewcell


    【解决方案1】:

    确保您的 tableview 控制器位于导航控制器的层次结构中或嵌入 UINavigationController。因为 UIAlertView 在 swift 中已被弃用并且有一个 UIAlertViewController 并且每当您需要或呈现警报时,您必须有一个控制器层次结构。

    您也可以使用相同的代码通过稍加更改 UIAlertControllerStyle 来显示 ActionSheet。

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    相关资源
    最近更新 更多