【问题标题】:Multiple buttons with same segueIdentifer具有相同 segueIdentifer 的多个按钮
【发布时间】:2014-10-28 17:28:49
【问题描述】:

我有一个带有一堆按钮的 tableviewController,当它们被点击时,我想推送到一个新的视图控制器,我正在使用 prepareForSegue 方法,然后在情节提要中使用控制拖动。

在prepareForSegue我有

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

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

提前感谢您的帮助。

编辑

- (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'

【问题讨论】:

    标签: ios objective-c segue uistoryboard


    【解决方案1】:

    您可以使用从 tableviewController 到另一个 viewController 的控件拖动来创建单个 segue,并在按钮的 IBAction 中触发 segue。您只能为所有按钮创建一个 IBAction,并使用 sender 参数来识别按钮。

    【讨论】:

    • 将 IBAction 连接到什么?我有点困惑,您能添加一些示例代码吗?
    【解决方案2】:

    您只需要一个 segue 标识符,它标识从一个 UIViewController 到另一个的移动。然后在“调用”UIViewcontroller(拥有 tableview 和按钮的 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 中的按钮但这是一种让多个按钮触发相同序列而无需在多个位置显式分配序列标识符的方法。它的另一个好处是能够在调用 -performSegueWithIdentifier:sender 之前为每个按钮执行自定义功能,从而为您的应用增加灵活性。

    希望这会有所帮助!

    【讨论】:

    • 是的,我让它工作了,在我的 prepareForSeugue 中我有这样的东西,UIButton *senderButton=(UIButton *)sender;DiningItemsTableViewController *destVC=(DiningItemsTableViewController *)segue.destinationViewController; 然后有destVC.mealNumber = senderButton.tag % 10;,它似乎没有发送餐号?
    • 或我发送的任何其他引用按钮标签的数据
    • 发现必须在 [self performSegueWithIdentifier:@"showItem" sender:sender] 方法中包含此内容,以便数据发送感谢您的回答!! :)
    • 好的,很好。乐意效劳!玩得开心编码!
    猜你喜欢
    • 2021-03-02
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 2018-08-02
    • 2021-09-14
    相关资源
    最近更新 更多