2 回答

TA貢獻(xiàn)1942條經(jīng)驗(yàn) 獲得超3個(gè)贊
故事板ID是一個(gè)String字段,您可以使用該字段創(chuàng)建基于該Storyboard ViewController的新ViewController。一個(gè)示例用法來自任何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創(chuàng)建一個(gè)MyCustomViewController,并將其顯示在當(dāng)前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貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超4個(gè)贊
要添加到Eric的答案并為Xcode 8和Swift 3更新它:
故事板ID完全符合名稱的含義:它標(biāo)識(shí)。只是它識(shí)別故事板文件中的視圖控制器。這是故事板知道哪個(gè)視圖控制器是哪個(gè)。
現(xiàn)在,不要被名字搞糊涂。故事板ID不能識(shí)別“故事板”。根據(jù)Apple的文檔,故事板“代表了應(yīng)用程序全部或部分用戶界面的視圖控制器?!?nbsp;所以,當(dāng)你有類似下圖的內(nèi)容時(shí),你有一個(gè)名為Main.storyboard的故事板,它有兩個(gè)視圖控制器,每個(gè)視圖控制器都可以給出一個(gè)故事板ID(它們在故事板中的ID)。
您可以使用視圖控制器的故事板ID來實(shí)例化并返回該視圖控制器。然后,您可以按照自己的意愿操縱并呈現(xiàn)它。要使用Eric的示例,假設(shè)您想在按下按鈕時(shí)呈現(xiàn)帶有標(biāo)識(shí)符“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 關(guān)注
- 1388 瀏覽
添加回答
舉報(bào)