Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-29-2010, 07:23 PM   #1 (permalink)
Registered Member
 
DenVog's Avatar
 
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 624
DenVog is on a distinguished road
Question Switch View Controller and Pass Button Info

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.

ViewControllerA.m:
Code:
- (IBAction)buttonOnePressed:(id)sender {
setButton.tag = 1;
BViewController *bViewController = [[BViewController alloc] init];
[bViewController.buttonPressedTag = setButton.tag]; // always warning or error
[bViewController setModalPresentationStyle:UIModalPresentationFormSheet];
[self presentModalViewController:bViewController animated:YES];
[bViewController release], bViewController = nil;
}
ViewControllerB.h:
Code:
@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];
}
Suggestions appreciated.
DenVog is offline   Reply With Quote
Old 09-29-2010, 09:43 PM   #2 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 386
SoulRed12 is on a distinguished road
Default

Two things.

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.

©2011 Dardom Productions | Like us on Facebook!
SoulRed12 is offline   Reply With Quote
Old 09-29-2010, 11:34 PM   #3 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

The tag should probably be defined before you get to this method. As written, there is no reason to use the tag.

You may want to avoid naming things starting with set. set implies a setter, which doesn't seem to be the case.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 09-30-2010, 10:49 AM   #4 (permalink)
Registered Member
 
DenVog's Avatar
 
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 624
DenVog is on a distinguished road
Smile

Thanks for your reply.
Quote:
Originally Posted by SoulRed12 View Post
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 View Post
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.
DenVog is offline   Reply With Quote
Old 09-30-2010, 11:02 AM   #5 (permalink)
Registered Member
 
DenVog's Avatar
 
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 624
DenVog is on a distinguished road
Talking

Quote:
Originally Posted by BrianSlick View Post
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 View Post
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.
DenVog is offline   Reply With Quote
Old 09-30-2010, 11:05 AM   #6 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

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.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 09-30-2010, 12:38 PM   #7 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 386
SoulRed12 is on a distinguished road
Default

Quote:
Originally Posted by DenVog View Post
Thanks for your reply.

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:

Code:
-(id)initWithButtonTag:(int)tag {
   if (self = [super init]) {
      self.buttonPressedTag = tag;
   }
   return self; //Must always return self on init methods
}
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.

But as others are saying, you shouldn't have a variable or pointer prefixed with "set", for the reasons BrianSlick mentioned.
__________________
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.

©2011 Dardom Productions | Like us on Facebook!
SoulRed12 is offline   Reply With Quote
Old 09-30-2010, 12:49 PM   #8 (permalink)
Registered Member
 
DenVog's Avatar
 
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 624
DenVog is on a distinguished road
Default

Quote:
Originally Posted by SoulRed12 View Post
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.
Doh! That got rid of the error. I seem to have tried everything else, don't know why that did not occur to me.



Quote:
Originally Posted by SoulRed12 View Post
If it's the latter, then try this. Create a custom init method on the BViewController class, like this:

Code:
-(id)initWithButtonTag:(int)tag {
   if (self = [super init]) {
      self.buttonPressedTag = tag;
   }
   return self; //Must always return self on init methods
}
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.
DenVog is offline   Reply With Quote
Old 10-01-2010, 05:16 PM   #9 (permalink)
Registered Member
 
DenVog's Avatar
 
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 624
DenVog is on a distinguished road
Red face No Logging in viewDidAppear

Quote:
Originally Posted by SoulRed12 View Post
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.

Arrgggh. That was frustrating!

Thanks for your help.
DenVog is offline   Reply With Quote
Old 10-01-2010, 05:18 PM   #10 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

You can log anywhere. If the log wasn't appearing, the method isn't being called.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 10-01-2010, 05:25 PM   #11 (permalink)
Registered Member
 
DenVog's Avatar
 
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 624
DenVog is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
You can log anywhere. If the log wasn't appearing, the method isn't being called.
Hmm. Well I thought you should be able to as well. However, I'm using the following, and I never see it in the log:
Code:
- (void)viewDidAppear:(BOOL)animated {
	NSLog(@"View did appear.");
	[super viewDidAppear:animated];
}
I don't know what would prevent that method from getting called.
DenVog is offline   Reply With Quote
Old 10-01-2010, 05:28 PM   #12 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

You're usually using addSubview: and it doesn't get called very often doing that.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Reply

Bookmarks

Tags
uibutton

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 341
6 members and 335 guests
headkaze, mistergreen2011, nobstudio, Objective Zero, revg, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,655
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, pungs
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:10 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0