gpt4 book ai didi

ios - 当 View Controller 超过 5 个时,为什么设置选项卡栏 Controller 的 selectedViewController >= 4 会抛出错误?

转载 作者:行者123 更新时间:2023-11-29 00:41:36 26 4
gpt4 key购买 nike

我有一个以编程方式导航的 UITabBarController。调用如下方法:

// UITabBarController subclass

- (void)tabForward
{
NSUInteger selectedIndex = [self.viewControllers indexOfObject:self.selectedViewController];
selectedIndex = selectedIndex + 1;
if ( selectedIndex < [self.viewControllers count] )
self.selectedViewController = [self.viewControllers objectAtIndex:selectedIndex];
}

最初似乎工作正常,但随后同一行 (self.selectedViewController = [self.viewControllers objectAtIndex:selectedIndex];) 抛出错误,[__NSArrayM insertObject:atIndex:] : object cannot be nil, when there are more than 5 view controllers and selectedIndex == 4. 但是,如果我只是从选项卡栏 Controller 中删除任何 View Controller 以使总数达到 5 个 View Controller 或下面,不会抛出任何错误。这让我抓狂。有谁知道我做错了什么吗?

最佳答案

// UITabBarController subclass

- (void)tabForward
{
NSInteger selectedIndex = [self.viewControllers indexOfObject:self.selectedViewController];
NSInteger nextIndex = selectedIndex + 1;
if (nextIndex < self.viewControllers.count) {
if (nextIndex > 3) { // use navigation controller when selectedIndex >= 4
self.selectedViewController = self.moreNavigationController;
[self.moreNavigationController pushViewController:self.viewControllers[nextIndex] animated:YES];
} else {
self.selectedViewController = self.viewControllers[nextIndex];
}
}
}

关于ios - 当 View Controller 超过 5 个时,为什么设置选项卡栏 Controller 的 selectedViewController >= 4 会抛出错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39354607/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com