2016-10-28 3 views
3

ナビゲーションバーの前のボタンからビューが閉じられています。私は、ビューのスタックからビューがポップされているということを正しく言います。今実際には私は彼/彼女のアドレスを設定するユーザーを求めるUIAlertを表示したいビューを実際に却下する前に。ビューを閉じる前にUIAlertを表示

は、私はこれを試してみたが、UIAlertはショーされていません。

2016-10-28 16:58:47.380 gaifong[17096:14515422] Warning: Attempt to present <UIAlertController: 0x7fd7037e4cb0> on <gaifong.ProfileViewController: 0x7fd7048cc000> whose view is not in the window hierarchy! 
+0

は、あなたのコントローラーが提示またはプッシュされているのですか? – KKRocks

+0

これがプッシュされています –

+0

この回答を確認してください:http://stackoverflow.com/a/34343418/2172546 – t4nhpt

答えて

1

あなたはnavigationItemにleftBarButtonItemを追加し、にアクションを行う必要があります印刷されます、次の私のコンソールで

override func viewWillDisappear(animated: Bool) { 
    if let currentUser = ApiManager.sharedInstance.currentUser { 
     if !currentUser.hasAddress { 
      let alert = UIAlertController(title: "Missing address", message: "We see you're stilling missing an address, would you like set it now?", preferredStyle: UIAlertControllerStyle.Alert) 

      alert.addAction(UIAlertAction(title: "yes".localized, style: .Default, handler: { (alertAction) in 
       let newViewController = LocationViewController() 
       newViewController.delegate = self 
       self.navigationController?.pushViewController(newViewController, animated: true) 
      })) 

      alert.addAction(UIAlertAction(title: "no".localized, style: .Default, handler: nil)) 

      self.presentViewController(alert, animated: true, completion: nil) 
     } 
    } 
} 

leftBarButtonItemこれを扱うことができます。 viewDidLoad

self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(self.goBack)) 

// Then handle the button selection 
func goBack() { 
    if let currentUser = ApiManager.sharedInstance.currentUser { 
    if !currentUser.hasAddress { 
     let alert = UIAlertController(title: "Missing address", message: "We see you're stilling missing an address, would you like set it now?", preferredStyle: UIAlertControllerStyle.Alert) 

     alert.addAction(UIAlertAction(title: "yes".localized, style: .Default, handler: { (alertAction) in 
      // here you can pop to main controller 
      self.navigationController?.popViewControllerAnimated(true) 
     })) 

     alert.addAction(UIAlertAction(title: "no".localized, style: .Default, handler: nil)) 

     self.presentViewController(alert, animated: true, completion: nil) 
    } 
} 
} 
+0

私が戻ったときにgoBack()関数は起動しません。ユーザーがスワイプして戻る場合はどうすればよいですか?私は別の機能を実装する必要がありますか? –

+0

これをトリガーする必要があります。その場合には動作しません。 – Sahil

関連する問題