I have ViewController A with two buttons. After pressing a button on View Controller A, I need to launch ViewController B and make it aware which of the two buttons was pressed. This is not a navigation controller.
Launching the second view controller is no sweat. I'm guessing button tag is the attribute to distinguish between the buttons. I can never get the button value to recognize in ViewController B though.
@interface BViewController : UIViewController {
int buttonPressedTag;
}
@property (nonatomic) int buttonPressedTag;
ViewControllerB.m:
Code:
@synthesize buttonPressedTag;
- (void)viewDidLoad {
if (self.buttonPressedTag == 1)
{
NSLog(@"It was button One. Do something specific to button one.");
}
else if (self.buttonPressedTag == 2)
{
NSLog(@"It was button Two. Do something specific to button two.");
}
[super viewDidLoad];
}
First, what is the warning or error you receive on the line you marked in the first bit of code? That could be a separate issue in and of itself.
Second, I'm 99% sure that ViewControllerB's viewDidLoad will actually occur BEFORE ViewControllerA makes it to the line where it sets ViewControllerB's buttonPressedTag property. Instead, you're going to want to put the code in ViewControllerB's viewWillAppear method.
__________________
HEY! Was this post helpful?
If so, it would be MUCH appreciated if you'd just click on one of these apps:
MyD
Take 1 minute to set up your MyD and you'll always be able to prove you own your device!
Membrik
Test your memory by sliding tiles to match chains of increasing difficulty.
First, what is the warning or error you receive on the line you marked in the first bit of code? That could be a separate issue in and of itself.
The Error I get with what's posted above is "Expected ':' before ']' token. I've tried some other implementations at that line, but they all warn or error. For example using setValue warns "invalid receiver type 'int'.
Quote:
Originally Posted by SoulRed12
Second, I'm 99% sure that ViewControllerB's viewDidLoad will actually occur BEFORE ViewControllerA makes it to the line where it sets ViewControllerB's buttonPressedTag property. Instead, you're going to want to put the code in ViewControllerB's viewWillAppear method.
Thanks for that suggestion. It's not fixed this particular issue, but it did address another area I was working around by setting a delay in viewDidLoad.
The tag should probably be defined before you get to this method. As written, there is no reason to use the tag.
Hi Brian. Can you please clarify? I've been doing quite a bit of searching on this topic, and found several examples that use myButton.tag or setTag within the IBAction to identify the button. Maybe they are bad examples?
Quote:
Originally Posted by BrianSlick
You may want to avoid naming things starting with set. set implies a setter, which doesn't seem to be the case.
Good suggestion. I could see how this would be confusing. Thanks.
Yes, in terms of a conditional. if (tag == whatever). So the tag already has a value. You are assigning the value in the IBAction. The tag would be assigned in IB, or perhaps viewDidLoad.
The Error I get with what's posted above is "Expected ':' before ']' token. I've tried some other implementations at that line, but they all warn or error. For example using setValue warns "invalid receiver type 'int'.
Just noticed it:
Code:
[bViewController.buttonPressedTag = setButton.tag]; // always warning or error
This line shouldn't have square brackets around it. You only need them when you're sending a message to an instance.
Quote:
Thanks for that suggestion. It's not fixed this particular issue, but it did address another area I was working around by setting a delay in viewDidLoad.
In what way did it not fix the issue? Was the main issue the error on the above line? Or that the bViewController didn't seem to be getting the value of the button's tag?
If it's the latter, then try this. Create a custom init method on the BViewController class, like this:
And be sure to add the method signature to the .h file:
Code:
-(id)initWithButtonTag:(int)tag;
Then instead of calling alloc/init, call [[BViewController alloc] initWithButtonTag:setButton.tag];
That will DEFINITELY assign to the variable before the bViewController checks for it.
Hmm. I quickly threw this in there, but the BViewController is still not getting the message. I need to look at it in more detail this afternoon to make sure I've not done something else silly. Thanks for your replies.
In what way did it not fix the issue? Was the main issue the error on the above line? Or that the bViewController didn't seem to be getting the value of the button's tag?
I found the issue. After removing the brackets, it is in fact working. Even without the additional init method. The problem was I was watching for which button tag was pushed via NSLog in the viewDidAppear method of ViewControllerB. I guess you can not log in viewDidAppear?! Once I moved that check after viewDidLoad, I now see which button was pressed.