gpt4 book ai didi

objective-c - 标签栏替换 View Controller

转载 作者:行者123 更新时间:2023-12-02 04:12:38 26 4
gpt4 key购买 nike

我想在某些特定条件下从选项卡栏 Controller 替换 View Controller 。这是我的代码。

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{

NSInteger selectIndex=[tabBarController.viewControllers indexOfObject:viewController];

if (selectIndex==2) {

UserObject * user=[[SharedClass sharedInstance] getUser];
if (user.license_no.length>0 && user.insurance_no.length>0) {

OfferRideVC *vc=[self.storyboard instantiateViewControllerWithIdentifier:@"OfferRideVC"];

NSMutableArray *allviews=[[NSMutableArray alloc] initWithArray:[tabBarController viewControllers]];
[allviews removeObjectAtIndex:selectIndex];
[allviews insertObject:vc atIndex:selectIndex];
[tabBarController setViewControllers:allviews];

}


}

return YES;
}

但是我的应用程序因此错误而崩溃。'NSInvalidArgumentException',原因:'-[UITabBarController setSelectedViewController:]只能选择选项卡栏 Controller 的 View Controller 列表中的 View Controller 。'

任何人都可以知道我的代码有什么问题吗?

最佳答案

发生这种情况是因为您在函数末尾仍然返回YES,因此 TabBar 尝试选择现在不在其 ViewControllers 列表中的 viewController。

从您的 if 案例中返回 NO

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{

NSInteger selectIndex=[tabBarController.viewControllers indexOfObject:viewController];

if (selectIndex==2) {

UserObject * user=[[SharedClass sharedInstance] getUser];
if (user.license_no.length>0 && user.insurance_no.length>0) {

OfferRideVC *vc=[self.storyboard instantiateViewControllerWithIdentifier:@"OfferRideVC"];

NSMutableArray *allviews=[[NSMutableArray alloc] initWithArray:[tabBarController viewControllers]];
[allviews removeObjectAtIndex:selectIndex];
[allviews insertObject:vc atIndex:selectIndex];
[tabBarController setViewControllers:allviews];

// ViewControllers changed, return NO
return NO;
}


}

return YES;
}

关于objective-c - 标签栏替换 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35794323/

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