如果用户需要登录才能访问某项功能,但是当我按下警告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
}
没有错误。它被调用,好像我输入一个不存在的id我得到了没有segue与指定的标识符发现错误消息。
UIAlert是否会影响performSegueWithIdentifier()
以下帖子使用这种方法,所以我认为它应该有效。我错过了什么。
** IB截图**
修改
我已经尝试移动self.performSegueWithIdentifier("unwindToLogin", sender:self)
以查看确实已显示但仍然是鼻涕工作。
答案 0 :(得分:0)
块内的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)
}
}))
答案 1 :(得分:0)
看到你没有提供足够的信息,我向你展示了这个'解决方案'。
我认为这就足够了。
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)
})
}
}