Ios4 以编程方式设置setSelectedIndex不会选择ViewController

Ios4 以编程方式设置setSelectedIndex不会选择ViewController,ios4,Ios4,在我的iOS应用程序中,当我设置setSelectedIndex时,它不会调用 以下函数 (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController 有什么替代方案吗?来自: tabBarController:didSelectViewController: “[…]它仅在用户点击选项卡栏时被调用,而在代码以编程方

在我的iOS应用程序中,当我设置setSelectedIndex时,它不会调用 以下函数

(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
有什么替代方案吗?

来自:

tabBarController:didSelectViewController:

“[…]它仅在用户点击选项卡栏时被调用,而在代码以编程方式更改选项卡栏内容时不被调用。”

因此,我建议您找到另一种方法来实现所需的功能


一个选项是将
tabBarController:didSelectViewController:
中的任何逻辑移动到视图控制器中的另一个方法,并在委托方法(如上)中调用该方法,在UITabBarController实例上运行
setSelectedIndex
的代码中。

如果您需要在选项卡更改时响应,而不管它是否来自点击,我建议覆盖setSelectedIndex:

-(void)setSelectedIndex:(NSUInteger)selectedIndex
{
    [super setSelectedIndex:selectedIndex];
    [self handleSelectionChanged];
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    [self handleSelectionChanged];
}

- (void)handleSelectionChanged
{
    // Do something
}

没有看到你问题的背景。您需要在类中创建一个新方法,并在调用
setSelectedIndex
时调用该方法,或者直接在调用
setSelectedIndex
的地方包含任何逻辑。总之,
setSelectedIndex
永远不会触发
didSelectViewController
,也不应该尝试以编程方式调用该委托方法。您需要考虑另一种解决方案。已实施,但我现在遇到了一个崩溃2012-08-13 16:44:12.979 iOSFileShare[33922:207]***由于未捕获的异常“NSInternalInconsistencException”而终止应用程序,原因是:“正在进行转换时尝试从开始模式转换到”。等待ViewDidDisplay/ViewDidEnglishe,以了解当前转换已完成“***第一次抛出时调用堆栈:哇,看起来您有一个bug!这看起来有些不同。如果你不能解决这个问题,我建议你再做一个描述(用代码!)。如果我的回答回答了你的问题,你能接受吗?谢谢:)不行,霍斯!你应该提出一个新问题——而且在StackOverflow上有比我聪明得多的人!