2012-02-12 42 views
169

在我的應用我有一個以編程執行賽格瑞按鈕:編程執行Segue公司和參數傳遞給目標視圖

- (void)myButtonMethod 
{ 
    //execute segue programmatically 
    [self performSegueWithIdentifier: @"MySegue" sender: self]; 
} 

我想知道是否有參考目標視圖的方式和傳遞一些參數。

我知道在prepareForSegue方法中,我可以參考它:myDestinationViewController *vc = [segue destinationViewController];,但我不知道如何以編程方式執行segue。

你有什麼想法嗎?

感謝,yassa


UPDATE:

我對這個問題很抱歉!我只是發現,即使以編程方式調用segue,仍然會調用prepareForSegue方法,因此可以按照相同的常規方式傳遞參數。

+44

添加更新作爲一個答案,並接受它,讓人們知道這已經回答了:) – danielbeard 2012-05-11 03:52:38

+0

添加答案,通知發表評論。 – DanSkeel 2012-05-28 08:16:13

+11

您應該將[答案](http://stackoverflow.com/a/10783047/377384)標記爲正確,以便人們知道它已被回答。 – 2012-09-06 05:05:41

回答

104

答案很簡單,就是如何觸發segue沒有區別。

在任何情況下都調用prepareForSegue:sender:方法,這是您傳遞參數的地方。

90

老問題,但這裏是關於如何做你所問的代碼。在這種情況下,我將數據從表視圖中的選定單元格傳遞到另一個視圖控制器。

@property(weak, nonatomic) NSObject* dataModel; 
在.m文件

在trget鑑於.h文件

@synthesize dataModel; 

dataModel可以stringint,或像在這種情況下,它是一個模型,包含許多項目

- (void)someMethod { 
    [self performSegueWithIdentifier:@"loginMainSegue" sender:self]; 
} 

OR。 ..

- (void)someMethod { 
    UIViewController *myController = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeController"]; 
    [self.navigationController pushViewController: myController animated:YES]; 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if([segue.identifier isEqualToString:@"storyDetailsSegway"]) { 
     UITableViewCell *cell = (UITableViewCell *) sender; 
     NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 
     NSDictionary *storiesDict =[topStories objectAtIndex:[indexPath row]]; 
     StoryModel *storyModel = [[StoryModel alloc] init]; 
     storyModel = storiesDict; 
     StoryDetails *controller = (StoryDetails *)segue.destinationViewController; 
     controller.dataModel= storyModel; 
    } 
} 
0

我明白在一個地方執行segue並保持狀態發送參數準備segue的問題。

我想出了一個方法來做到這一點。我已經使用一個類別向ViewControllers添加了一個名爲userInfoDict的屬性。我也重寫了帶有標識符的segue,以這種方式,如果發件人是self(意味着控制器本身)。它會將這個userInfoDict傳遞給下一個ViewController。

這裏不是傳遞整個UserInfoDict,而是可以傳遞特定參數作爲發送者並相應地覆蓋。

1件事你需要牢記。不要忘記在你的performSegue方法中調用超級方法。

0

如果您使用新的swift版本。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if segue.identifier == "ChannelMoreSegue" { 

     } 
}