2 回答

TA貢獻1942條經驗 獲得超3個贊
故事板ID是一個String字段,您可以使用該字段創建基于該Storyboard ViewController的新ViewController。一個示例用法來自任何ViewController:
//Maybe make a button that when clicked calls this method- (IBAction)buttonPressed:(id)sender{ MyCustomViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"MyViewController"]; [self presentViewController:vc animated:YES completion:nil];}
這將基于您命名為“MyViewController”的故事板ViewController創建一個MyCustomViewController,并將其顯示在當前View Controller之上
如果您在app代理中,則可以使用
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
編輯:斯威夫特
@IBAction func buttonPressed(sender: AnyObject) { let vc = storyboard?.instantiateViewControllerWithIdentifier("MyViewController") as MyCustomViewController presentViewController(vc, animated: true, completion: nil)}
編輯Swift> = 3:
@IBAction func buttonPressed(sender: Any) { let vc = storyboard?.instantiateViewController(withIdentifier: "MyViewController") as! ViewController present(vc, animated: true, completion: nil)}
和
let storyboard = UIStoryboard(name: "MainStoryboard", bundle: nil)

TA貢獻1785條經驗 獲得超4個贊
要添加到Eric的答案并為Xcode 8和Swift 3更新它:
故事板ID完全符合名稱的含義:它標識。只是它識別故事板文件中的視圖控制器。這是故事板知道哪個視圖控制器是哪個。
現在,不要被名字搞糊涂。故事板ID不能識別“故事板”。根據Apple的文檔,故事板“代表了應用程序全部或部分用戶界面的視圖控制器?!?nbsp;所以,當你有類似下圖的內容時,你有一個名為Main.storyboard的故事板,它有兩個視圖控制器,每個視圖控制器都可以給出一個故事板ID(它們在故事板中的ID)。
您可以使用視圖控制器的故事板ID來實例化并返回該視圖控制器。然后,您可以按照自己的意愿操縱并呈現它。要使用Eric的示例,假設您想在按下按鈕時呈現帶有標識符“MyViewController”的視圖控制器,您可以這樣做:
@IBAction func buttonPressed(sender: Any) { // Here is where we create an instance of our view controller. instantiateViewController(withIdentifier:) will create an instance of the view controller every time it is called. That means you could create another instance when another button is pressed, for example. let vc = storyboard?.instantiateViewController(withIdentifier: "MyViewController") as! ViewController present(vc, animated: true, completion: nil)}
- 2 回答
- 0 關注
- 1377 瀏覽
添加回答
舉報