0

I am working on an iOS application where I have two separate buttons that call, and display a UIViewController from an .xib file. When this viewController is displayed for the user, the user enters data, and then dismisses the viewController, and is taken back to the main application where he/she originally came from.

My problem is that this viewController that is called, and then dismissed by the user is activated by two different buttons, and the data that is entered by the user has to be kept track based on which button calls it. However, in creating and calling the viewController, I don't know how to pass the tag value from the buttons which would distinguish which button is calling it.

Here is my code which creates and calls the viewController from the .xib file (and is called by both buttons):

- (IBAction)buttonClicked:(id)sender {
    
    _nextView = [[NextViewController alloc] initWithNibName:@"NextViewController" bundle:nil];
    [_nextView setDelegate:(id)self];
    NextNavigationController *navigationController = [[NextNavigationController alloc] initWithRootViewController:_nextView];
    [self presentViewController:navigationController animated:YES completion:nil];
    
}

This code works fine in calling the viewController, but I also need to pass with it the tag value from the button which calls this method. How can I do this?

4

4 回答 4

2
- (IBAction)buttonClicked:(UIButton *)sender {

    _nextView = [[NextViewController alloc] initWithNibName:@"NextViewController" bundle:nil];
    [_nextView setDelegate:(id)self];
    _nextView.buttonTag = sender.tag;
    NextNavigationController *navigationController = [[NextNavigationController alloc] initWithRootViewController:_nextView];
    [self presentViewController:navigationController animated:YES completion:nil];

}

buttonTag 将是您在 _nextView 中创建的整数属性

于 2013-08-29T15:49:00.947 回答
0

您可以从发件人本身访问标签值

NSInteger tag = [(UIButton *)sender tag];

问候

于 2013-08-29T15:48:21.960 回答
0

我没有对此进行测试,但你不能将发送者对象转换为 uicontrol 然后获取标签吗?

UIControl *view=(UIControl *)sender;
NSInteger tagNo=view.tag;
于 2013-08-29T15:48:28.227 回答
0

sender是对按钮的引用。只需将发件人的类型更改为(UIButton*),您应该能够检查您想要的标签

- (IBAction)buttonClicked:(UIButton*)sender {

    NSInteger tag = sender.tag;
    (...)

}
于 2013-08-29T15:49:17.127 回答