繁体   English   中英

在UIAlert中取消segue无法正常工作

[英]unwind segue in UIAlert not working

我试图向用户显示警报,如果他们需要登录才能访问功能,但是当我按警报中的登录按钮self.self.performSegueWithIdentifier("tryonToLogin", sender:self)似乎没有做任何东西。

LoginViewController

@IBAction func unwindToLogin(segue:UIStoryboardSegue){

}

UIAlert

 @IBAction func goToGallery(sender: UIButton){
    if(isLoggedIn){
        performSegueWithIdentifier("ShowGallery", sender: sender)
    }else{
        showMessage("You must login to view access this feature", sender:sender)
    }
}

func showMessage(popUpMessageText : String, sender:UIButton){
    let messageTitle = "Error"

    print("prepreform segue")
    performSegueWithIdentifier("unwindToLogin", sender:sender)

    let refreshAlert = UIAlertController(title: messageTitle, message: popUpMessageText, preferredStyle: UIAlertControllerStyle.Alert)

    refreshAlert.addAction( UIAlertAction(title: "Login", style: .Default, handler: { (action: UIAlertAction!) in
        print("Handle Login redirect logic here")
        self.performSegueWithIdentifier("unwindToLogin", sender: self)
        print("after Handle Login redirect logic here")
    }))

    refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: { (action: UIAlertAction!) in
        print("Handle Cancel logic here")
    }))


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

}

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    print("prepare for segue called")
    self.camera = nil
}

没有错误。 它正在被调用,好像ii放在一个不存在的id中一样。 UIAlert是否会影响performSegueWithIdentifier()的调用

以下帖子使用这种方法,因此我认为它应该起作用。 我想念什么。

** IB屏幕截图**

屏幕截图1

在此处输入图片说明

编辑

我尝试移动self.performSegueWithIdentifier("unwindToLogin", sender:self)进行查看,但仍然无法正常工作。

块中的self是UIAlertController,而不是UIViewController。 创建对UIViewController的弱引用,然后在该对象上调用方法。

weak var weakSelf = self

let refreshAlert = UIAlertController(title: messageTitle, message: popUpMessageText, preferredStyle: UIAlertControllerStyle.Alert)

refreshAlert.addAction( UIAlertAction(title: "Login", style: .Default, handler: { (action: UIAlertAction!) in
    print("Handle Login redirect logic here")
    dispatch_async(dispatch_get_main_queue()) {
        weakSelf?.performSegueWithIdentifier("unwindToLogin", sender:self)
    }
}))

鉴于您没有提供足够的信息,我向您介绍此“解决方案”。

  • 创建UIViewController的扩展,以实施设施功能以实现alertView-
  • 我想您将代码放在“ viewDidLoad”上:您必须在“ viewDidAppear”上移动代码。
  • 请检查您在prepareForSegue中执行的操作

我认为这足够了。

extension UIViewController {

    /**
     Show alert view with OK/Cancel button.

     - parameter title:       alert title
     - parameter message:     alert message
     - parameter onOk:        callback on ok button tapped
     - parameter onCancel:    callback on cancel button tapped

     - returns: alert controller
     */
    func showAlert(title title: String, message: String, onOk: (() -> ())?, onCancel: (() -> ())? = nil) -> UIAlertController {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
        let okAction = UIAlertAction(title: "Ok", style: .Default) { (alert: UIAlertAction!) -> Void in
            onOk?()
        }
       let cancelAction = UIAlertAction(title: "Cancel"), style: .Default) { (alert: UIAlertAction!) -> Void in
            onCancel?()
        }
        alertController.addAction(okAction)
        alertController.addAction(cancelAction)
        self.presentViewController(alertController, animated: true, completion: nil)
        return alertController
    }

}

class LoginViewController: UIViewController {
      override func viewDidAppear(animated: Bool) {
         super.viewDidAppear(animated) 
         let title = "Login"
         let message = messageTitle
         self.showAlert(title: title, message: message, onOk: { () -> () in
            self.performSegueWithIdentifier("unwindToLogin", sender: self)
         })  
      }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM