【问题标题】:unwind segue in UIAlert not workingUIAlert 中的 unwind segue 不起作用
【发布时间】:2016-10-01 00:11:15
【问题描述】:

如果用户需要登录才能访问某项功能,我会尝试向用户显示警报,但是当我按下警报中的登录按钮时,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 我得到 no segue with specified identifier found 错误消息。 UIAlert 是否影响performSegueWithIdentifier()的调用

下面的帖子使用了这种方法,所以我认为它应该可以工作。我错过了什么。

**IB 截图**

编辑

我尝试移动 self.performSegueWithIdentifier("unwindToLogin", sender:self) 以查看确实出现了,但它仍然无法正常工作。

【问题讨论】:

  • 你最终让它工作了吗?我也不能让它工作。
  • @bibscy 不幸的是我不记得解决方案,并且不再可以访问该项目

标签: ios swift uialertcontroller unwind-segue


【解决方案1】:

看到你没有提供足够的信息,我给你这个“解决方案”。

  • 创建 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)
         })  
      }
}

【讨论】:

  • 对不起,但不明白这如何适用于我的问题。而且您没有说明您使用这种方法的来源
  • 已编辑...我真的认为这段代码会对你有所帮助,没有理由让它不工作。
  • 我认为这可能是过度杀戮,因为我的问题似乎是 self.performSegueWithIdentifier("unwindToLogin", sender: self) 在任何地方都不起作用。我知道它被调用的唯一方法是传递一个无效的 id
【解决方案2】:

块内的 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)
    }
}))

【讨论】:

  • 我在想这样的事情可能是问题,但它仍然不起作用。
  • 添加一个 func tmp() { print("tmp") } 方法到你的 ViewController 并调用 weakSelf?.tmp() 来检查是否调用了 tmp 方法。
  • 好的,那么问题出在 performSeque 上。注释掉警报并调用 self.performSegueWithIdentifier("unwindToLogin", sender:self) 检查它是否正常工作。还要检查 IB 中序列的名称。
  • 我错了,block里面的self指的是UIViewController,你根本不需要weakSelf这个东西。
  • 我添加了带有 segue 的屏幕截图。我也尝试将performSegueWithIdentifier("unwindToLogin", sender: self) 移动到 viewDidLoad() 作为测试,但它仍然不起作用。我想我可能在某个地方忘记了一些小的基本步骤
猜你喜欢
  • 1970-01-01
  • 2014-10-28
  • 1970-01-01
  • 2015-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多