2013-03-17 87 views
3

我有兩個問題。在兩個不同的故事板之間傳遞數據

第一個問題

在我的項目,我有兩個不同的故事板文件:A storyboardB storyboard

我想從(A) controller of (A) storyboard數據(nsstring)傳遞給(B) controller of (B) storyboard

我怎麼做呢?

第二個問題

在第二故事板

我有兩個控制器賽格瑞

鏈當我在該指令的代碼調用SEGUE:

[self.navigation controller performSegueWithIdentifier: @"secondViewSegue" sender:self]; 

我有一個消息: "has no segue with identifier 'secondViewSegue' "

爲什麼?

+0

兩個故事板?除了iPad和iPhone的不同屏幕尺寸有不同的故事板以外,在單個架構上分離故事板還有什麼實際用途?這甚至有可能嗎?如果你想要在用戶界面中保持靈活性,爲什麼不使用XIB呢? – 2013-03-17 19:21:26

+0

@MichaelDautermann完全支持多個故事板。儘管默認情況下加載了初始情節提要,但可以根據需要加載其他情節提要。有可行的用例。 – GoZoner 2013-03-17 19:24:47

回答

3

1 /這樣做的一個好方法是製作一個單獨的模型對象,可以在兩個位置進行同樣的處理。而最簡單的方法來做到是一個屬性添加到您的AppDelegate.h文件的@interface部分,例如:

@property (nonatomic, strong) NSString* sharedString; 

來設置和獲取它,你需要類型訪問任何文件添加到AppDelegate中說需要它:

#include AppDelegate.h 

然後可以使用...

AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; 

    //to set (eg in A controller) 
    appDelegate.sharedString = string; 

    //to get (eg in B controller) 
    NSString* string = appDelegate.sharedString; 

作爲替代的屬性,你可以在頭文件中使用靜態變量:

static NSString* staticString; 

這將是訪問的任何對象,#imports頭文件。儘管不是Objective-C的方式。

對於更詳細的情況,您可能需要創建一個單例對象來訪問模型數據。

2/TRY:

[self performSegueWithIdentifier: @"secondViewSegue" sender:self]; 

確保Segue公司從你的viewController有線,不是的導航控制器。

+0

第一個問題你可能是對的,唯一的解決辦法是在AppDelegate中共享變量.. 第二個問題好的,我解決了 謝謝 – giorgio83 2013-03-17 21:01:38

+0

很好,謝謝 – Ronaldoh1 2015-06-14 05:52:25

1

在不同的故事板中,有兩種不同的視圖控制器之間傳遞數據的方法。我使用的是什麼,我想在接下來的ViewController分享

斯威夫特3

let storyboard = UIStoryboard(name: "StoryboardName2", bundle: nil) 
    let controller :NextViewController = storyboard.instantiateViewController(withIdentifier: "NextVCIdentifier") as! NextViewController 
    controller.NameofUser = Self.SelectedUser 

聲明變量作爲

public var NameofUser:String = "" 
相關問題