2013-06-01 61 views
0
AView.m 
     UIButton *btn = [[UIButton alloc]initWithFrame()]; 
     [btn addTarget:self action:@selectior(perpareForSegue:sender:) forControlEvents:UIControlEventTouchUpInside]; 
     [self.view addSubview:btn]; 


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 
     //I want to get destinationViewController of segue with identifier"toNextView" 
     // and then perform the segue 
     // Difficulties that I encounter is that I have a NSInvalidArgumentException 
     // Is that mean the view controller doesn't know the existence of the segue? 
     // I have connected two controller with a segue 
} 

我想用標識符「toNextView」賽格瑞的 destinationViewController,然後執行SEGUE 困難,我遇到的是,我有一個NSInvalidArgumentException 是指視圖控制器不知道SEGUE的存在? 我已連接兩個控制器與一個segue 謝謝如何以編程方式指定segue(不執行segue)?

回答

0

看看class reference of UIStoryboardSegue

該方法prepareForSegue:發件人:由故事板運行時在觸發segue時調用。這並不意味着被稱爲執行segue。

下面是從類引用的解釋:

當SEGUE被觸發,但視覺轉變發生之前, 故事板運行時調用當前視圖控制器的 prepareForSegue:發送方:方法使得它可以通過任何需要的數據到 即將顯示的視圖控制器。

從類參考:

您仍然可以啓動賽格瑞編程方式使用 performSegueWithIdentifier:發件人:如果UIViewController中的方法,你想 。您可能會這樣做,以編程方式添加 ,因此在Interface Builder中不可用,從而啓動segue。

所以,只需指定按鈕的選擇,然後裏面選擇就叫performSegueWithIdentifier:發件人:方法。

[btn addTarget:self action:@selector(whateverAction) forControlEvents:UIControlEventTouchUpInside]; 

- (void)whateverAction { 
    [self performSegueWithIdentifier:@"toNextView" sender:nil]; 
} 

如果你想要一個對象傳遞給SEGUE的目的地視圖控制器,這是其中prepareForSegue:發件人:方法進場時在課堂上引用解釋,例如:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"toNextView"]) { 
     DestinationViewController *vc = (DestinationViewController *)[segue destinationViewController]; 
     //do whatever you want to do with vc, including passing of data, setting property, etc. 
    } 
} 
+0

謝謝,但我可以在將數據傳遞給destiniation視圖控制器時執行SegueWithIdentifier嗎? – user2429523

+0

@ user2429523這就是prepareForSegue方法的用法。我更新了我的答案。 –

+0

非常好...它可以解決,另外問題:如何將點擊按鈕的標籤發送到目標視圖控制器? – user2429523