PDA

View Full Version : UITabBar - Need high level explanation




Earendil
Oct 20, 2009, 12:31 AM
Okay, so I'm new to Objective C, Xcode, and thus also the iPhone SDK :)
However I'm not to new programming or OO thinking, so feel free to explain things in terms of regular SE ideas.

So I've written my first little app. I use a UITabBar to flip between two screens, the main functionality of the app, and a page of preferences.

Everything has worked fine, and I'm starting to get a hang of things, but here is where my understanding of what is actually going on all breaks down.

On the preferences page is a button, that when pressed needs to change a label on the the first page. I have an event that captures the button press just fine, but when it goes to change the label the program crashes. I'm not great with debugging things in Xcode yet, but if I had to hazard a guess, I'd say it was some sort of scope problem?

I'm 99.99% sure I have everything connected properly in IB, as I am fully able to create a button in a tab that modifies a label within the currently selected tab. It's modifying a tab that isn't currently being viewed that has me stuck.

When a UITabBar is used, does only one "tab" exist at a time? This might explain it.

My second thought was "I'll just store the preference state in a few variables, and upon loading the first tab, use those variables to modify the UI!". However I can't find any sort of "on tab load" event.

I've searched high and low for an answer, and now I'm "giving up" and coming to you guys for help. It's always the worst when you can't find an answer to a seemingly simple problem, because that usually means it's stupid-simple.



ghayenga
Oct 20, 2009, 10:34 AM
Okay, so I'm new to Objective C, Xcode, and thus also the iPhone SDK :)
However I'm not to new programming or OO thinking, so feel free to explain things in terms of regular SE ideas.

So I've written my first little app. I use a UITabBar to flip between two screens, the main functionality of the app, and a page of preferences.

Everything has worked fine, and I'm starting to get a hang of things, but here is where my understanding of what is actually going on all breaks down.

On the preferences page is a button, that when pressed needs to change a label on the the first page. I have an event that captures the button press just fine, but when it goes to change the label the program crashes. I'm not great with debugging things in Xcode yet, but if I had to hazard a guess, I'd say it was some sort of scope problem?

I'm 99.99% sure I have everything connected properly in IB, as I am fully able to create a button in a tab that modifies a label within the currently selected tab. It's modifying a tab that isn't currently being viewed that has me stuck.

When a UITabBar is used, does only one "tab" exist at a time? This might explain it.

My second thought was "I'll just store the preference state in a few variables, and upon loading the first tab, use those variables to modify the UI!". However I can't find any sort of "on tab load" event.

I've searched high and low for an answer, and now I'm "giving up" and coming to you guys for help. It's always the worst when you can't find an answer to a seemingly simple problem, because that usually means it's stupid-simple.

The viewControllers in those two separate tabs don't necessarily know anything about each other.

There's a bunch of different ways you could communicate that information from one to the other. Set variables on the shared application delegate and use those to reference them and send messages to each other. If you're using a tabBarController it has an array of the viewControllers it's managing, have one view register for a notification and the other post the notification when it gets the event. Set up key-value observing on a global variable so when viewController in tab 2 changes the value viewController in tab 1 detects it, etc.

drf1229
Oct 20, 2009, 07:13 PM
Ok first of all, are you correctly using the IBAction? Second, did you connect the two views in Interface builder? If both of those are correct in both code and interface builder, you need to assign the label you are changing as a property. Doing this is simple. In the view with the label header file at the bottom but before @end, put this:

@property(nonatomic, retain) UILabel *YourLabelName;

This way you can access the label through any view that is connected to the view with the label via IBOutlet. The reason for the crash is most likely an misconnection in interface builder, so double check you don't get any errors/warnings in interface builder. If problems continue, feel free to inquire, I'd be glad to help.