这应该是如此简单......但是有些事情正在发生.
我的设置如下:
MainViewController
Tab Bar Controller
4 tabs, each of which loads WebViewController
Run Code Online (Sandbox Code Playgroud)
我的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]
Run Code Online (Sandbox Code Playgroud)
除了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 = …Run Code Online (Sandbox Code Playgroud)