繁体   English   中英

在父视图控制器中修改选项卡栏项目

[英]Modifying tab bar items in parent view controller

在prepareForSegue期间是否可以在父视图控制器中修改选项卡栏项目? 基本上,我正在尝试在父控制器中插入逻辑,在此逻辑中,可能会在Segue中删除2个可能的标签栏项之一。

我已经尝试了以下方法:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"identifierOfInterest"]) {
        UITabBarController *tabBarController = (UITabBarController *)segue.destinationViewController;

        // Attempt 1
        NSMutableArray *items = [[tabBarController.tabBar items] mutableCopy];
        [items removeObjectAtIndex:1];
        [tabBarController.tabBar setItems:items animated:YES];

        // Attempt 2
        NSMutableArray *items = [[tabBarController viewControllers] mutableCopy];
        [items removeObjectAtIndex:1];
        [tabBarController setViewControllers:items];
    }
}

不幸的是,两条消息都使应用程序崩溃,并出现以下错误:

尝试1:

Directly modifying a tab bar managed by a tab bar controller is not allowed.

尝试2:

[UITabBarItem parentViewController]: unrecognized selector sent to instance

我使用的是Xcode 4.2,iOS 5,UI标签栏控制器是情节提要对象,因此标签栏没有任何出口。

提前致谢!

通常,在TabBar应用程序中,由于AppDelegate是“主”视图控制器,因此它拥有(或具有对它)引用。 您不能固定到标签栏视图控制器,或者至少不应该。 如果需要让应用程序的标签栏控制器执行某些操作,请向App Delegate实例发送一条消息,该实例应具有对“标签栏控制器”的引用,以执行工作。

//In View Controller handling seque
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    [[[UIApplication sharedApplication] delegate] performSelector:@selector(doTabBarWork:) withObject:data];
}

//In the App delegate
-(void)doTabBarWork:(id)data
{
    //The "mainTabBarController" instance needs to already be set
    NSMutableArray *viewControllers =  [[self.mainTabBarController viewControllers] mutableCopy];
    [viewControllers removeLastObject];
    [self.mainTabBarController setViewControllers:viewControllers animated:YES];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM