Reputation: 2809
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?
Upvotes: 0
Views: 799
Reputation: 11675
sender
is a reference to the button. Just change the type of the sender to (UIButton*)
and you should be able to check for the tag you want
- (IBAction)buttonClicked:(UIButton*)sender {
NSInteger tag = sender.tag;
(...)
}
Upvotes: 0
Reputation: 104082
- (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 would be an integer property you create in _nextView
Upvotes: 2
Reputation: 4077
I have not tested this but can't you cast the sender object to a uicontrol and then get the tag ?
UIControl *view=(UIControl *)sender;
NSInteger tagNo=view.tag;
Upvotes: 0
Reputation: 387
You can access the tag value from the sender itself
NSInteger tag = [(UIButton *)sender tag];
Regards
Upvotes: 0