2011-09-29 44 views
6

在兩個視圖控制器之間實現segue時如何使用segue對象更改目標視圖控制器的屬性?文檔說這可以在prepareForSegue:sender:方法中完成。我嘗試過,但不是沒有成功iOS 5 segue實現

回答

13

說不上來,如果你仍然需要一個答案,但它是這樣一個孤獨的職位,如果我是正確的,這並不NDA下摔倒下去。如果我錯了,請將我的答案遺忘一下,所以我們走了:我剛剛完成了一些使用你需要的東西。這是代碼爲我的作品:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([[segue identifier] isEqualToString:@"relevantSegueIdentifier"]) 
    { 
     // [segue destinationViewController] is read-only, so in order to 
     // write to that view controller you'll have to locally instantiate 
     // it here: 
     ViewController *upcomingViewController = [segue destinationViewController]; 

     // You now have a solid reference to the upcoming/destination view 
     // controller. Example use: Allocate and initialize some property of 
     // the destination view controller before you reach it and inject a 
     // reference to the current view controller into the upcoming one: 
     upcomingViewController.someProperty = [[SomePropertyClass alloc] initWithString:@"Whatever!"]; 
     upcomingViewController.initialViewController = [segue sourceViewController]; 
     // Or, equivalent, but more straightforward: 
     //upcomingViewController.initialViewController = self; 
    } 
} 

這假定兩個someProperty和initialViewController合成目標視圖控制器的存取。希望這可以幫助!

+0

它屬於NDA,但在3天內它不會正式發佈在十月十四日,但我喜歡你的答案+1 –

+0

我試過你說的,但我的'destinationViewController'上的屬性不更新。有什麼建議? – Francesco

+0

我認爲你使用目標視圖控制器的綜合訪問器(而不是私有ivar),並且視圖控制器已經在應用程序設置期間實例化(例如,具有實際視圖控制器的故事板放置在它中間,它們之間的所有實例化應用程序發射)。在這種情況下,它應該工作。這可能就像系統不知道你的特定目標。視圖控制器類。在我的例子中,類ViewController是UIViewController的一個子類,在@implementation之前我做了一些類似'#import「ViewController.h」'的操作。它可能是這些東西之一嗎? – DarqueSandu

0

我在源視圖控制器使用的是:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    UIViewController *upcomingViewController = [segue destinationViewController]; 
    upcomingViewController.view.tag = [[segue identifier] hash]; 
} 

然後在目的地視圖控制器我使用(在viewDidAppear使用例如)

if(self.view.tag == [@"MySeqgueIdentifier" hash]) 
{ 
    // Do something here... 
} 

這是很酷的,因爲你不必創建任何屬性等等,並且所有的東西都可以在界面構建器中正常工作。