objective c - 呈现视图控制器时出错

时间:2014-01-16 21:31:09

标签: ios objective-c uinavigationcontroller storyboard uitabbarcontroller

我试图从两个不同的角度展示一个视图控制器,我将在一秒钟内解释:

  1. 我放置在标签栏中的自定义按钮
  2. 来自UIBarButtonItem
  3. 两种方式都会给我以下错误:

    Warning: Attempt to present <UINavigationController: 0x14e627c10> on <UINavigationController: 0x14e6211e0> whose view is not in the window hierarchy!
    

    我会把“很少”的代码行放在可能相关的位置:

    标签栏的初始化:

    - (void)presentTabBarController {
        UIStoryboard *tabbarStoryboard = [UIStoryboard storyboardWithName:@"MainTabBarViews" bundle:nil];
    
        self.feedViewController = [tabbarStoryboard instantiateViewControllerWithIdentifier:@"GSFeedViewController"];
        self.exploreViewController = [tabbarStoryboard instantiateViewControllerWithIdentifier:@"GSExploreViewController"];
        self.createGroupViewController = [tabbarStoryboard instantiateViewControllerWithIdentifier:@"GSCreateGroupViewController"];
        self.messagingViewController = [tabbarStoryboard instantiateViewControllerWithIdentifier:@"GSMessagingViewController"];
        self.profileViewController = [tabbarStoryboard instantiateViewControllerWithIdentifier:@"GSProfileViewController"];
    
        self.tabBarController = [[UITabBarController alloc]init];
    
        UINavigationController *feedNavigationController = [[UINavigationController alloc]initWithRootViewController:self.feedViewController];    
        UINavigationController *exploreNavigationController = [[UINavigationController alloc]initWithRootViewController:self.exploreViewController];
        UINavigationController *createGroupNavigationController = [[UINavigationController alloc]initWithRootViewController:self.createGroupViewController];
        UINavigationController *messagingNavigationController = [[UINavigationController alloc]initWithRootViewController:self.messagingViewController];
        UINavigationController *profileNavigationController = [[UINavigationController alloc]initWithRootViewController:self.profileViewController];
    
        UITabBarItem *feedItem = [[UITabBarItem alloc]init];
        feedItem.image = [UIImage imageNamed:@"tabbar-feed-icon1"];
        feedItem.title = @"Timeline";
        [feedNavigationController setTabBarItem:feedItem];
    
        UITabBarItem *exploreItem = [[UITabBarItem alloc]init];
        exploreItem.image = [UIImage imageNamed:@"tabbar-explore-icon1"];
        exploreItem.title = @"Explore";
        [exploreNavigationController setTabBarItem:exploreItem];
    
        UITabBarItem *messageItem = [[UITabBarItem alloc]init];
        messageItem.image = [UIImage imageNamed:@"tabbar-message-icon1"];
        messageItem.title = @"Messages";
        [messagingNavigationController setTabBarItem:messageItem];
    
        UITabBarItem *profileItem = [[UITabBarItem alloc]init];
        profileItem.image = [UIImage imageNamed:@"tabbar-profile-icon1"];
        profileItem.title = @"Profile";
        [profileNavigationController setTabBarItem:profileItem];
    
    
        self.tabBarController.viewControllers = @[feedNavigationController, exploreNavigationController, createGroupNavigationController, messagingNavigationController, profileNavigationController];
        self.tabBarController.tabBar.translucent = YES;
    
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *buttonImage = [UIImage imageNamed:@"tabbar-group-icon"];
        UIImage *highlightImage = [UIImage imageNamed:@"tabbar-group-selected-icon"];
        button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
        button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width -0, buttonImage.size.height -1);
        button.clipsToBounds = YES;
        button.userInteractionEnabled = YES;
        button.layer.borderColor = [[UIColor blackColor]CGColor];
        button.layer.borderWidth = 0.5f;
        //button.layer.cornerRadius = buttonImage.size.width / 6;
        [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
        [button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
        [button addTarget:self action:@selector(popCreateGroupModel) forControlEvents:UIControlEventTouchUpInside];
    
    
        CGFloat heightDifference = buttonImage.size.height - self.tabBarController.tabBar.frame.size.height;
        if (heightDifference < 0)
            button.center = self.tabBarController.tabBar.center;
        else
        {
            CGPoint center = self.tabBarController.tabBar.center;
            center.y = center.y - heightDifference/2.0;
            button.center = center;
        }
    
        [self.tabBarController.view addSubview:button];
    
        self.navController = [[UINavigationController alloc]init];
        [self.navController setViewControllers:@[self.tabBarController] animated:NO];
    }  
    

    弹出呈现ViewController

    -(void)popCreateGroupModel
    {
        UINavigationController *nav = [[UINavigationController alloc]
                                       initWithRootViewController:self.createGroupViewController];
        [[self.tabBarController navigationController] presentViewController:nav animated:YES completion:nil];
    }
    

    这是第一种方式,第二种方式就是我说出一个呈现VC的按钮。

    - (IBAction)openNewGroupModal:(id)sender {
        UINavigationController *nav = [[UINavigationController alloc]
                                       initWithRootViewController:[GSAppDelegate sharedDelegate].createGroupViewController ];
        [[[[GSAppDelegate sharedDelegate] tabBarController] navigationController] presentViewController:nav animated:YES completion:nil];
    }
    

    最后但并非最不重要:我的AppDelegate的属性

    @interface GSAppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>
    
    @property (strong, nonatomic) UIWindow *window;
    @property (nonatomic, strong) UITabBarController *tabBarController;
    @property (nonatomic, strong) UINavigationController *navController;
    @property (strong, nonatomic) Device *deviceInformation;
    @property (nonatomic, strong) GSFeedViewController *feedViewController;
    @property (nonatomic, strong) GSExploreViewController *exploreViewController;
    @property (nonatomic, strong) GSCreateGroupViewController *createGroupViewController;
    @property (nonatomic, strong) GSMessagingViewController *messagingViewController;
    @property (nonatomic, strong) GSProfileViewController *profileViewController;
    
    @property (nonatomic, readonly) int networkStatus;
    
    - (BOOL)isParseReachable;
    + (GSAppDelegate *)sharedDelegate;
    - (void)presentTabBarController;
    @end
    

    我尝试了stackoverflow返回给我的有关此错误的所有解决方案。任何建议表示赞赏!

    故事板:

    非常简单。我通过代码来做最多的事情。

    enter image description here

0 个答案:

没有答案