iPhone:选项卡的 selectedIndex 值应该一致,但事实并非如此
这本来应该很简单……但奇怪的事情发生了。
我的设置如下所示:
MainViewController
Tab Bar Controller
4 tabs, each of which loads WebViewController
我的 AppDelegate 包含一个 ivar tabBarController,它连接到选项卡栏控制器(这都是在 Interface Builder 中设置的)。最左边的选项卡在 IB 中标记为“已选择”。
在 WebViewController 的 viewWillAppear 方法中,我需要知道刚刚选择了哪个选项卡,以便加载正确的 URL。我通过打开 appDelegate.tabBarController.selectedIndex 来做到这一点。
当应用程序首次运行并选择最左侧的选项卡时,selectedIndex 是一个很大的垃圾值。之后,我得到从 0 到 3 的值,这是应该的,但它们是随机顺序的。不仅如此,我触摸的每个选项卡每次都会报告不同的值。
这个应用程序现在极其简单,我无法想象我可以做什么让事情变得如此糟糕。
有没有人见过(并希望解决)这种行为?
更新:我们有一个代码请求。没什么可看的。
标签栏控制器在 applicationDidFinishLaunching 中加载:
[self.mainViewController view]; //force nib to load
[self.window addSubview:self.mainViewController.tabBarController.view]
除了 tabBarController 的合成和释放之外,目前 MainViewController.m 中没有任何代码。
来自 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];
}
这是我看到随机值的地方。顺便说一句,S_UIDelegate 只是一个返回指向应用程序委托的指针的宏。我正在使用其他人的应用程序模板,而他是 #define 的忠实粉丝,可以节省打字时间。
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. BTW, S_UIDelegate is just a macro that returns a pointer to the app delegate. I'm using someone else's app template and he's a huge fan of #define to save on typing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一位同事刚刚给出了答案。 viewWillAppear 中尚未设置 selectedIndex;当我改用 viewDidAppear 时,它按预期工作。
A co-worker just provided the answer. selectedIndex isn't set up yet in viewWillAppear; when I switched to using viewDidAppear instead, it works as expected.
您应该尝试将 switch case 移动到 tabBarController delegate 中:
– tabBarController:shouldSelectViewController:
– tabBarController:didSelectViewController:
You should try to move your switch case into a tabBarController delegate :
– tabBarController:shouldSelectViewController:
– tabBarController:didSelectViewController: