簡體   English   中英

如何將數據傳遞到 Xcode 中的另一個故事板?

[英]How to pass data to another storyboard in Xcode?

在 iPhone 應用程序中,如何將數據從一個視圖(xib 文件)傳遞到另一個故事板?

例如,

我有“welcome.xib”,其中包含 nameTextfield 和 nextButton ,用戶將他們的名字輸入到 nameTextfield 中,然后單擊下一步

該應用程序將導航到“main.storyboard”並在“main.storyboard”中的 userNameLabel 上顯示 nameTextfield.text 中的文本

到目前為止我所知道的:

  • 我知道如何在視圖之間傳遞數據(從一個 xib 文件到另一個 xib 文件)
  • 我知道如何從視圖(xib 文件)導航到另一個故事板

覆蓋要從中傳遞信息的 ViewController 中的 -prepareForSegue:sender: 方法。

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    DetailViewController *sc = [segue destinationViewController];
    // Here you can set variables in detailViewController. For example if you had a NSString called name
    sc.name = @"name_here";
    // Or you could do
    [sc setName:@"name_here"];
}

斯威夫特 4.2+

將此添加到要從中發送數據的視圖控制器中 -

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        guard
            let destination = segue.destination as? viewControllerName
            else {
                return
        }
        destination.variable =data
    }

確保變量的名稱與您要將數據發送到的名稱相同,並且viewControllerName應該完全相似。

然后使用以下命令在單擊按鈕時或在任何 completionHandler 內執行操作 -

self.performSegue(withIdentifier: "viewControllerIdentifierName", sender: nil)

重要的是還要確保連接兩者的故事板應該具有與您在 withIdentifier 中鍵入的內容相同的標識符名稱,否則應用程序將崩潰。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM