多个按钮,具有相同的segueIdentifer

问题描述:

我有一个tableviewController与一堆按钮,当他们被挖掘我想推到一个新的视图控制器,我使用prepareForSegue方法,然后在故事中使用控制拖动板。多个按钮,具有相同的segueIdentifer

在prepareForSegue我

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([segue.identifier isEqualToString:@"showItems"]) { 
} 

然后一堆,我发送到下一个视图控制器的东西,所以在故事板上所有的按钮具有相同标识符“showItems”这是给我警告"Multiple segues have same identifer showItems"摆脱此警告的最佳方法是什么?我仍然希望所有的按钮都做同样的事情,推送下一个viewController是更好的做法。

感谢您的帮助提前。

EDIT

- (IBAction)showItems:(id)sender{ 

    [self performSegueWithIdentifier:@"showItem" sender:self]; 
} 

我已连接的所有按钮的IBAction为,但随后会在哪里我将数据传递到下一个视图控制器?

在PrepareForSegue我

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

UIButton *senderButton=(UIButton *)sender; 

NSInteger meal = senderButton.tag % 10; 
} 

但是,这将引发异常unrecognized selector sent to instance 0x7fcc1a496880 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DiningMealsTableViewController tag]: unrecognized selector sent to instance 0x7fcc1a496880'

您只需要一个segue标识符,它标识从一个UIViewController到另一个的移动。然后在“调用”UIViewcontroller(拥有桌面视图和按钮的UIViewController)中,您可以使用每个按钮将调用的自定义函数触发该Segue。

像这样:

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    // Create the buttons and assign them unique tags 
    // and assign them a custom click handler 
    UIButton *button1 = [[UIButton alloc] initWithFrame:button1Frame]; 
    button1.tag = 1; 
    [button1 addTarget:self action:@selector(handleButtonPress:) forControlEvents:UIControlEventTouchUpInside]; 

    UIButton *button2 = [[UIButton alloc] initWithFrame:button2Frame]; 
    button2.tag = 2; 
    [button2 addTarget:self action:@selector(handleButtonPress:) forControlEvents:UIControlEventTouchUpInside]; 

    // Add the buttons to the view or to your UITableViewCell 
    [self.view addSubview:button1]; 
    [self.view addSubview:button2]; 
} 

- (void)handleButtonPress:(id)sender { 

    // get the tag you assigned to the buttons 
    // to identifiy which one was pressed 
    UIButton *btn = sender; 
    int tag = btn.tag; 
    NSLog(@"You clicked button with tag: %d", tag); 

    if (tag == 1){ 
     NSLog(@"You clicked on Button1"); 
     // Do something custom here. 
    } else if (tag == 2) { 
     NSLog(@"You clicked on Button2"); 
     // Do something custom here. 
    } 

    // Now this is where all your buttons can call the same SegueIdentifier 
    [self performSegueWithIdentifier:@"showItems" sender:sender]; 

} 

很显然,我不是很了解你的应用程序的结构,和我的例子中添加按钮到根视图,在您的应用程序,你就会有这种相同的设置,只附加到您放置在您的UITableViewCell中的按钮,但是您可以通过多个按钮触发相同的seque而不必在多个位置显式分配seque标识符,这样就可以避免这种情况。在调用-performSegueWithIdentifier:sender之前,它能够为每个按钮执行自定义功能,从而为您的应用程序提供更多的灵活性。

希望这会有所帮助!

+0

是的,它确实,我得到它的工作,在我的prepareForSeugue我有这样的事情,'UIButton * senderButton =(UIButton *)sender;''DiningItemsTableViewController * destVC =(DiningItemsTableViewController *)segue.destinationViewController;'然后有'destVC.mealNumber = senderButton.tag%10;'它似乎不发送mealNumber? – iqueqiorio 2014-10-29 00:21:40

+0

或我发送的任何其他数据引用按钮的标签按下 – iqueqiorio 2014-10-29 00:22:22

+0

计算出它必须在方法'[self performSegueWithIdentifier:@“showItem”sender:sender]'这样的数据会发送感谢为答案! :) – iqueqiorio 2014-10-29 00:28:26

可以使用调速拖动从tableviewController其他的viewController创建一个单一的赛格瑞和火SEGUE中的IBAction为按钮。您只能为所有按钮创建一个IBAction,并使用sender参数来识别该按钮。

+0

我该如何连接IBAction?林有点困惑,你可能会添加一些示例代码? – iqueqiorio 2014-10-28 23:23:02

+0

我添加了迄今为止所获得的内容。编辑 – iqueqiorio 2014-10-28 23:29:48