iPhone: value of selectedIndex for tab should be consistent, but isn't
Posted
by Janine
on Stack Overflow
See other posts from Stack Overflow
or by Janine
Published on 2010-04-07T16:58:09Z
Indexed on
2010/04/07
17:23 UTC
Read the original article
Hit count: 400
This should be so simple... but something screwy is happening.
My setup looks like this:
MainViewController
Tab Bar Controller
4 tabs, each of which loads WebViewController
My AppDelegate contains an ivar, tabBarController, which is connected to the tab bar controller (this was all set up in Interface Builder). The leftmost tab is marked "selected" in IB.
Within the viewWillAppear method in WebViewController, I need to know which tab was just selected so I can load the correct URL. I do this by switching on appDelegate.tabBarController.selectedIndex.
When the app first runs and the leftmost tab is selected, selectedIndex is a large garbage value. After that, I get values from 0 to 3, which is as it should be, but they are in random order. Not only that, but each tab I touch reports a different value each time.
This app is extremely simple right now and I can't imagine what I could have done to make things go this wrong.
Has anyone seen (and hopefully solved) this behavior?
Update: we have a request for code. There's not much to see.
The tab bar controller gets loaded in applicationDidFinishLaunching:
[self.mainViewController view]; //force nib to load
[self.window addSubview:self.mainViewController.tabBarController.view]
There is currently no code whatsoever in MainViewController.m other than the synthesize and release for tabBarController.
From WebVewController.m:
- (void)viewWillAppear:(BOOL)_animation {
[super viewWillAppear:_animation];
NSURL *url;
switch([S_UIDelegate mainViewController].tabBarController.selectedIndex) {
case 0: url = [NSURL URLWithString:@"http://www.cnn.com"];
break;
case 1: url = [NSURL URLWithString:@"http://www.facebook.com"];
break;
case 2: url = [NSURL URLWithString:@"http://www.twitter.com"];
break;
case 3: url = [NSURL URLWithString:@"http://www.google.com"];
break;
default: url = [NSURL URLWithString:@"http://www.msnbc.com"];
}
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
This is where I'm seeing the random values.
© Stack Overflow or respective owner