iPhoneDev、UITabBarController SelectedIndex 问题
我正在尝试以编程方式切换选项卡栏项目。下面的代码工作正常,但我无法让它在某些情况下工作:
self.tabBarController.selectedIndex = 0;
问题是:
我有一个带有两个 UIViewController 类的选项卡栏控制器。在第二个选项卡中,我将上面的代码放置在 viewWillAppear 方法内的 if 语句内。上面的代码本身可以工作,但只有在视图从未加载过时才有效。所以,在第一个选项卡上我有一个按钮。当按下该按钮时,我以编程方式切换到第二个选项卡。那时我的代码可以工作,并且我基本上停留在第一个选项卡上。但第一次之后切换就不起作用了。如果我在按下该按钮之前选择第二个选项卡,它也不起作用。我的代码所在的 IF 语句会触发,但所选选项卡不会更改。
任何帮助将不胜感激。如果您需要,请随时要求澄清。
谢谢!
让我补充更多信息:
(1) 这仅在极少数情况下使用。第一个屏幕是搜索屏幕。按搜索后,您将自动进入第二个选项卡,除非没有结果,否则您会收到警报并停留在那里。
(2) 第二个选项卡维护所有项目的列表(如果未执行缩小范围的搜索)或缩小范围的项目的列表。无论哪种方式,该列表始终可用。因此,即使我在搜索页面上阻止他们,如果他们忘记并单击其他选项卡,他们也会被告知没有项目。此时,我想将它们带回选项卡一,只是为了方便起见。
这没什么大不了的,没有它我也可以生活,而且这也不是某些人所说的可怕的编程方案。我只是想添加一项用户便利项...
感谢 tc!我在 UITabBarControllerDelegate 中使用了以下方法:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
if(self.tabBarController.selectedIndex == 0){
if([books count] == 0){
[self alertNoResults];
return(FALSE);
}
}
return(TRUE);
}
非常有效!
I am trying to programmatically switch the tab bar item. The following code works fine, but I can't get it to work in certain situations:
self.tabBarController.selectedIndex = 0;
Here is the problem:
I have a tab bar controller with two UIViewController classes. In the second tab, I have the code above placed inside an if statement within the viewWillAppear method. The code above itself works, but it only works if the view has never been loaded. So, on the first tab I have a button. When that button is pushed, I programmatically switch to the second tab. At that point my code works, and I essentially stay on the first tab. But the switching doesn't work after that first time. It also doesn't work if I select that second tab before I press that button. The IF statement that my code is inside fires, but the selected tab doesn't change.
Any help would be appreciated. Please don't hesitate to ask for clarification if you need it.
Thanks!
Let me add more information:
(1) This is only used on rare occassion. The first screen is a search screen. After you press search, you are taken to the second tab automatically, unless there are no results, then you are alerted and you stay there.
(2) The second tab maintains a list of either all items, if no search to narrow down has been performed, or the the list of narrowed down items. Either way, that list is always available. So even though I stop them on the search page, if they forget and click the other tab, they will be informed there are no items. At this point I would like to bring them back to tab one, as a convience only.
It's not THAT big of a deal, I can live without it, and it's not the horrible programming scheme that some are making it out to be. I just wanted to add that one user convienence item ...
Thanks to tc! I used the following method in my UITabBarControllerDelegate:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
if(self.tabBarController.selectedIndex == 0){
if([books count] == 0){
[self alertNoResults];
return(FALSE);
}
}
return(TRUE);
}
Worked like a charm!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在选项卡切换期间切换选项卡。 火腿医生
尝试实现 UITabBarDelegate 的
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
;如果没有结果则返回NO。或者,在
-viewDidAppear:
中进行检查(它更有可能起作用)。如果这不起作用,您甚至可以使用performSelectorInBackground(但要注意 - 用户可能仍然点击其他选项卡!)You're switching tabs during a tab switch. Ham doctor.
Try implementing UITabBarDelegate's
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
; return NO if there are no results.Alternatively, do the checking in
-viewDidAppear:
(it's more likely to work). If that doesn't work, you can even use performSelectorInBackground (but beware — the user might still tap other tabs!)