亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

什么是StoryBoard ID,我該如何使用它?

什么是StoryBoard ID,我該如何使用它?

波斯汪 2019-08-12 18:18:07
什么是StoryBoard ID,我該如何使用它?我是IOS的新手,最近開始使用Xcode 4.5。我看到每個viewController都可以設置一些身份變量,包括故事板ID。這是什么以及如何使用它?我開始在stackoverflow上搜索,但找不到任何解釋。我認為這不僅僅是一些愚蠢的標簽,我可以設置為記住我的控制器嗎?它有什么作用?
查看完整描述

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)


查看完整回答
反對 回復 2019-08-12
?
九州編程

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)}


查看完整回答
反對 回復 2019-08-12
  • 2 回答
  • 0 關注
  • 1377 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號