【问题标题】:How to display AlertController in UiView如何在 UiView 中显示 AlertController
【发布时间】:2015-05-26 04:38:28
【问题描述】:

Swift 新手。下面的代码执行没有错误,但在控制台中显示一条消息。这是一条错误消息以及如何修复它?发送应用审核时是否需要修复。

谢谢

这是我的应用结构:

VC(1) --> 转到 --> VC(2)

在 VC(1) 中我有:

  • BtnGo
  • UiLabelMsg
  • 可用视图

我创建了一个到 VC(2) 的链接:Control-Drag BtnGo 到 VC(2)

我将segue命名为:SegueToVC2

我想做的事

我需要将一个字符串传递给 VC2。在通过检查用户是否做出选择之前。

在 PrepareForSegue 中,

override func PrepareForSegue(segue:UIStoryboardSegue, sender: AnyObject!) {

    var strchk: String = UIlabelMsg.text!

    if strchk.isEmpty {

        var alert = UIAlertController(title: "Alert",message:" No Selection made",
           preferredStyle: UIAlertControllerStyle.Alert)

        alert.AddAction(UIAlertction(title: "OK",style:UIAlertActionStyle.Default,hanlder: nil))

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

    } else {

    if (segue.Identifier =="SegueToVC2") {

        var targetVC = segue.destinationViewControl as! VC2
        targetVC.strMsg = UILabelMsg.text
    }
}

问题

在控制台中,显示如下:

UIView : 0x7fdfc25668c0;帧 =(0,0,375,667);自动调整大小 = w+h;层 = 的窗口不等于视图的窗口!

我有这些问题

为什么我使用prepareForSegue时不需要为BtnGo编写代码? 如果我有超过 2 个按钮?这两个将由 Segue 处理?

【问题讨论】:

    标签: ios swift uiview uialertcontroller


    【解决方案1】:

    UIView 没有presentViewController 方法,self 是什么?您可以使用UIView 的窗口进行演示。

    let alert = UIAlertController(title: "Alert", message: "No Selection made", preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
    self.window?.rootViewController?.present(alert, animated: true, completion: nil)
    

    编辑:您可能需要覆盖shouldPerformSegueWithIdentifier 以检查UIlabelMsg.text

    override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {
        var strchk: String = UIlabelMsg.text!
        if strchk.isEmpty {
            var alert = UIAlertController(title: "Alert",message:" No Selection made",
            preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "OK",style:UIAlertActionStyle.Default,handler: nil))
            self.presentViewController(alert, animated: true, completion: nil)
    
            return false
        } else {
            return true
        }
    }
    

    【讨论】:

    • 窗口?不是会员。上面的函数在 PrepareForSegue 里面
    • @ Banings,请阅读。使用您的建议似乎无法解决问题。当我选择一个项目并按 BtnGo 时,没有错误。无选择时按BtnGo,控制台有Alert Msg,也有如上的错误码。
    • 试试这个:UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
    • 问题未解决。收到错误消息:尝试显示其视图不在 Windows 层次结构中的 UIalertController。致命错误:在展开可选值时意外发现 nil。
    • 第一个解决方案对我来说很好,但我做了更多:if let viewController = self.window?.rootViewController { viewController.present(alertController, animated: true) { }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-01
    相关资源
    最近更新 更多