2012-07-13 41 views
3

如何將值從一個控制器傳遞給另一個?我使用StoryBoard。用segue將數據傳輸到另一個控制器。從代碼

storyboard

我想這出現在第一個視圖中突出顯示的文本視圖。

調用的代碼的下一個觀點,我覺得這樣的事情應該是這樣的:

UIStoryboard *finish = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 

    UIViewController *viewController = [finish instantiateViewControllerWithIdentifier:@"FinishController"]; 

    viewController.modalPresentationStyle = UIModalPresentationPageSheet; 
    [self presentModalViewController:viewController animated:YES]; 

finishcontroller:

- (void)viewDidLoad 
{ 
    self.lblFinishTitle.text=self.FinishTitle; 
    self.lblFinishDesc.text = self.FinishDesc; 
    self.lblFinishPoint.text=self.FinishPoint; 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
} 

第一種觀點:

-(void) prepareForSegue:(UIStoryboardPopoverSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier hasPrefix:@"FinishController"]) { 
     FinishController *asker = (FinishController *) segue.destinationViewController; 
     asker.FinishDesc = @"What do you want your label to say?"; 
     asker.FinishTitle = @"Label text"; 
     asker.FinishPoint = @"asdas"; 
    } 
} 

我想通過導致代碼傳輸的值

回答

4

問題是您實際上並未使用該segue,而是使用presentModalController

請注意,通常情況下,您可以要求self提供故事板。然而,即使這是不必要的,當你連接了塞格斯:

[self preformSegueWithIdentifier:@"FinishController" sender:self]; 

然後prepareForSegue 被調用。還要注意的是,你可以(應該)使用的東西比SEGUE標識更權威,以確定是否應加載數據......你可以問SEGUE的目標控制器,如果它是正確的類:

-(void) prepareForSegue:(UIStoryboardPopoverSegue *)segue sender:(id)sender 
{ 
    if ([segue.destinationViewController isKindOfClass:[FinishController class]]) { 
     FinishController *asker = (FinishController *) segue.destinationViewController; 
     asker.FinishDesc = @"What do you want your label to say?"; 
     asker.FinishTitle = @"Label text"; 
     asker.FinishPoint = @"asdas"; 
    } 
} 

你是可能已經知道了(因爲您在代碼中使用了該標識符),但是爲了未來這篇文章的發現者的利益;當你在故事板中時,在Xcode的巡視器面板中會爲賽格賦予標識符。

+0

感謝您的快速回復!我意識到錯誤,但是當我使用:「[self preformSegueWithIdentifier:@」FinishController「];」,我得到一個錯誤:「沒有可見的@interface爲'ViewController'聲明選擇器'preformSegueWithIdentifier:'」 – Feor 2012-07-13 04:53:00

+0

對不起,我遺漏了方法簽名的一部分......它需要第二個參數(發送者),通常你會通過這個參數來導致發生segue(比如一個按鈕,比如......但是按鈕可以直接連接到segues故事板)。 http://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006926-CH3-SW83 – 2012-07-13 04:56:36

+0

[self performSegueWithIdentifier:@「Finish」發件人:自];用過的!一切都很好!非常感謝你! – Feor 2012-07-13 04:57:39

相關問題