【问题标题】:UIAlertView inside UIAlertAction in Swift 3.0 ?Swift 3.0 中的 UIAlertAction 内的 UIAlertView ?
【发布时间】:2017-04-05 09:14:02
【问题描述】:

是否可以在 UIAlertAction 中添加 UIAlert 视图?

因为当我尝试在 UIAlertAction 中添加 UIAlert 视图时,它说

"警告:尝试在其视图不在窗口中进行展示 等级制度!”

这是我的代码。

let myAlert = UIAlertController(title: "Title", message: "title here", preferredStyle: UIAlertControllerStyle.alert)
let okaction = UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler:
    {
        action in
        let myViewController:ViewController = self.storyboard!.instantiateViewController(withIdentifier: "ViewController") as! ViewController

        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let navigationController = UINavigationController.init(rootViewController: myViewController)
        appDelegate.window?.rootViewController = navigationController
        appDelegate.window?.makeKeyAndVisible()

        if (statement here == 1) {
            let myAlert = UIAlertController(title: "Title", message: "title", preferredStyle: UIAlertControllerStyle.alert)
            myAlert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil))
            self.present(myAlert, animated: true, completion: nil)
            return
        }
    }
)
myAlert.addAction(okaction)
self.present(myAlert, animated: true, completion: nil)

【问题讨论】:

    标签: swift3 appdelegate uialertcontroller uialertaction


    【解决方案1】:

    尝试在navigationController 上展示AlertController

    换行

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

    navigationController.present(myAlert, animated: true, completion: nil)
    

    【讨论】:

    • 假设我上面的代码在 SecondViewController 中,并且 if (statement == 1){} 正在使用上面的代码使其工作。但是,如果我在它下面还有另一个“if (statement == 2) {}”并且我想在不转到 rootViewController 的情况下显示警报怎么办。我的意思是它留在 SecondViewController 有可能吗?
    • @AlotJai 然后你需要在appDelegate.window?.rootViewController = navigationController appDelegate.window?.makeKeyAndVisible()里面写这两行if (statement == 1){}
    【解决方案2】:

    尝试将您对第二个警报的调用包装在一个函数中,然后调用该函数。

    像这样。

    let myAlert = UIAlertController(title: "Title", message: "title here", preferredStyle: UIAlertControllerStyle.alert)
    let okaction = UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: { action in
    
        let myViewController:ViewController = self.storyboard!.instantiateViewController(withIdentifier: "ViewController") as! ViewController
    
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let navigationController = UINavigationController.init(rootViewController: myViewController)
        appDelegate.window?.rootViewController = navigationController
        appDelegate.window?.makeKeyAndVisible()
    
        if (statement here == 1) {
            self.callAlert()
        }
    })
    
    myAlert.addAction(okaction)
    self.present(myAlert, animated: true, completion: nil)
    
    func callAlert() {
        let myAlert = UIAlertController(title: "Title", message: "title", preferredStyle: UIAlertControllerStyle.alert)
        myAlert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil))
        self.present(myAlert, animated: true, completion: nil)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多