2016-06-01 5 views
1

機能にアクセスするためにログインする必要があるが、警告のログインボタンを押したときにユーザーに警告を表示しようとしています。self.self.performSegueWithIdentifier("tryonToLogin", sender:self)は何もしていません。UIAlertで緊急処理を解除する

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を入れるかのように呼び出されています。指定された識別子を持つセグーが見つからないというエラーメッセージが表示されます。 UIAlertが呼び出しに影響しますperformSegueWithIdentifier()

次の記事ではこのアプローチを使用していますので、うまくいくはずです。私は何が欠けている。

** IBスクリーンショット**

screenshot 1

enter image description here

編集

私が表示されたビューにself.performSegueWithIdentifier("unwindToLogin", sender:self)を移動しようとしている、それはまだ鼻水の作業です。

答えて

0

ブロック内の自己はUIViewControllerではなくUIAlertControllerです。 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) 
    } 
})) 
+0

私はこれが問題であるかもしれないと考えていましたが、まだ動作しません。 – Lonergan6275

+0

あなたのViewControllerにfunc tmp(){print( "tmp")}メソッドを追加し、tmpメソッドが呼び出されたかどうかを調べるためにweakSelf?tmp()を呼び出します。 –

+0

はい、呼び出されます。 – Lonergan6275

0

あなたは十分な情報を提供していないことを見て、この「解決策」を提示します。

  • alertView-
  • を実装するために、施設の機能を実現するためにのUIViewControllerの拡張を作成します。私は、あなたが「の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) 
     }) 
     } 
} 
+0

私は申し訳ありませんが、これは私の質問にどのように当てはまりません。あなたはこのアプローチであなたがどこから来ているのか明確にしていませんでした – Lonergan6275

+0

...私は本当にこのコードがあなたを助けると思う、それがうまくいかない理由はありません。 –

+0

私の問題は 'self.performSegueWithIdentifier(" unwindToLogin "、sender:self)'であると思われるので、これはこれを超過する可能性があると思います。私はそれが呼び出されていることを知っている唯一の方法は、有効ではないIDを渡すことです – Lonergan6275

関連する問題