在 iOS 9 的 alert.addAction 中按下 OK 時如何打開另一個視圖控制器 (How to open another view controller when OK is pressed in alert.addAction in iOS 9)


問題描述

在 iOS 9 的 alert.addAction 中按下 OK 時如何打開另一個視圖控制器 (How to open another view controller when OK is pressed in alert.addAction in iOS 9)

當按下 add.alertAction 中的“OK”時,我想顯示一個名為 InViewController 的視圖控制器。

if ((user) != nil) {               
   let alert = UIAlertController(title: "Success", message:   "Logged In", preferredStyle: .Alert)
   alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
   self.presentViewController(alert, animated: true){}
}

## 參考解法 #### 方法 1:

You can add a completionHandler to the UIAlertAction when you add it to do it what you want, like in the following way:

if ((user) != nil) {               
    let alert = UIAlertController(title: "Success", message:   "Logged In", preferredStyle: .Alert)

    let OKAction = UIAlertAction(title: "OK", style: .Default, handler: { _ ‑> Void in 
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("ViewControllerA") as! ViewControllerA
        self.presentViewController(nextViewController, animated: true, completion: nil)
    })

    alert.addAction(OKAction)
    self.presentViewController(alert, animated: true){}
}

To set the StoryboardID you can use Interface Builder in the Identity Inspector, see the following picture:

enter image description here

I put everything in the above code referencing ViewControllerA, you have to set the name of your UIViewController according what you want.

EDIT:

You are pointing to a UIView or some other object on the StoryBoard. Press the yellow indicator on top of the other objects which is your UIViewController, like in the following picture:

enter image description here

I hope this help you.

方法 2:

let alert = UIAlertController(title: "Success", message: "Logged In", preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: .Default) { (action) ‑> Void in
  let viewControllerYouWantToPresent = self.storyboard?.instantiateViewControllerWithIdentifier("SomeViewControllerIdentifier")
  self.presentViewController(viewControllerYouWantToPresent!, animated: true, completion: nil)
}
alert.addAction(action)
self.presentViewController(alert, animated: true, completion: nil)

方法 3:

Here's how you can do that , I'm just updating the good work of Victor Sigler

you follow his answer with this little update ..

 private func alertUser( alertTitle title: String, alertMessage  message: String )
{
   let alert = UIAlertController(title: title, message: message,   preferredStyle: .alert)
    let actionTaken = UIAlertAction(title: "Success", style: .default) { (hand) in
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let destinationVC = storyBoard.instantiateViewController(withIdentifier: "IntroPage") as? StarterViewController
        self.present(destinationVC!, animated: true, completion: nil)

    }
    alert.addAction(actionTaken)
    self.present(alert, animated: true) {}
}

(by John HollidayVictor SiglerAndré SlottaHarvester Haidar)

參考文件

  1. How to open another view controller when OK is pressed in alert.addAction in iOS 9 (CC BY‑SA 2.5/3.0/4.0)

#iOS #swift






相關問題

將 jpg UIImage 轉換為位圖 UIImage? (convert jpg UIImage to bitmap UIImage?)

C++ Xcode iOS 項目錯誤 (C++ Xcode iOS project errors)

如何將以下行添加到我的 plist 文件中? (How can I add the following lines to my plist file?)

XCode 4.6 顯示方法 + (void)beginAnimations:(NSString *)animationID context:(void *)context; 的內存洩漏警告 (XCode 4.6 shows a warning of memory leak for method + (void)beginAnimations:(NSString *)animationID context:(void *)context;)

隨機分佈精靈 (randomly distribute sprites)

在 iOS 9 的 alert.addAction 中按下 OK 時如何打開另一個視圖控制器 (How to open another view controller when OK is pressed in alert.addAction in iOS 9)

在復選框上單擊獲取表行 ID - 目標 c (On check box click get the table Row ID - Objective c)

提交應用程序 - 添加本地化,但我的語言未列在組合框中? (Submitting app - Add Localizations, but my language is not listed in the combo box?)

是否可以從命令行更新 iOS 配置文件? (Can iOS Provisioning Profiles be renewed from the command-line?)

如何為collectionviewcell的刪除自定義動畫? (How to custom animation for collectionviewcell's deletion?)

由 LPLinkView 和 UIImageView 組成的單元格的 CollectionView 速度很慢,並且在滾動時會重新加載數據 (CollectionView with Cells made up of LPLinkView's and UIImageView's is Slow and Reloads Data While Scrolling)

刪除 tableview 單元格不會更新索引 (Deleting tableview cells won't update index)







留言討論