【问题标题】:title of navigationController that called the tabbarcontroller调用 tabbarcontroller 的 navigationController 的标题
【发布时间】:2014-05-22 11:53:38
【问题描述】:

我的应用故事板如下

它是一个基于标签栏控制器的应用程序,其中一个标签嵌入在导航控制器中。当用户单击第一个选项卡 (view1) 并单击此视图内的按钮时,他将被移动到 view2。而不是使用后退按钮返回到 view1,我希望用户单击选项卡项以返回到 view1 工作正常。但是,我想在用户单击选项卡并且他在 View2 中时查看警报。我正在使用 shouldSelectViewControllerdidSelectViewController 委托方法来检查单击了哪个选项卡并查看警报。问题是我无法从委托中的这些方法访问 View2 以通知应用程序仅在用户在 view2 中并单击选项卡时查看警报。

我尝试在 shouldSelectViewController 中使用此代码

    if (tabBarController.selectedIndex == 0) {
    NSLog(@"Delegate nav title: %@", tabBarController.selectedViewController.navigationItem.title);
}

这些行总是返回 view1 的标题

【问题讨论】:

  • 我遇到了同样的问题。现在我解决了这个问题。你能看到链接 [Tabbar 组件用来解决这个问题][1] [1]:stackoverflow.com/questions/23698697/…
  • 谢谢帮忙,不过好像不是同一个问题。我正在尝试从 AppDelegate 获取有关堆叠在 NavigationController 中的 viewController 的信息

标签: ios xcode navigationcontroller


【解决方案1】:

因为它们在同一个导航控制器中,所以它们具有相同的导航项。每个视图都可以配置这个导航项,但通常是同一个对象。这就是为什么它返回相同的标题。尝试在第二个视图控制器的 viewDidLoad 方法中设置导航项标题。

【讨论】:

    【解决方案2】:

    我终于找到了解决问题的方法 首先,我向 View2 添加了一个静态变量,并将其命名为 inView2,如下所示 在 View2.h

    @interface
    {}
    
    + ( BOOL ) isInQuizViewController;
    + ( void )setInQuizViewController:(BOOL)inQuizVal;
    //...
    @end
    

    在 View2.m 中

    @implementation
       static BOOL inView2 = NO;
    
    +(BOOL)isInView2
    {
        return inView2;
    }
    + ( void )setInView2:(BOOL)Val
    {
        inView2 = val;
    }
    

    这两种方法用于设置和获取 inView2 的值,它告诉我用户当前是否在 View2 中

    在 View1.h 中创建一个与按钮关联的 IBAction,该按钮将从 View1 传输到 View2 并将其连接到您的故事板

    - (IBAction)GoToView2:(id)sender;
    

    转到 View1.m 并导入

    #import "View2.h"
    

    并实现您的 IBAction 方法以将 InView2 设置为 YES

    - (IBAction)GoToView2:(id)sender {
    [View2 setInView2:YES];
    }
    

    然后在delegate.m中

    #import "View2.h"
    @implementation AppDelegate
    
    UITabBarController * _tabBarController;
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        _tabBarController = (UITabBarController *)_window.rootViewController;
        _tabBarController.delegate = self;
    
        // Override point for customization after application launch.
        return YES;
    }
    

    我定义了一个全局 _tabBarController 并将其设置为 _window.rootViewController 并将其委托设置为此委托并记住导入“View2.h”

    转到 ShouldSelectViewController 方法(请注意,此方法在过渡到所选选项卡 ViewController 之前调用,因此它非常适合决定是否应将所选选项卡 viewController 显示到用户)。 所以在 ShouldSelectViewController 方法中我做了以下

    - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    
    
    
        if(tabBarController.selectedIndex == 2)
        {
            if ([View2 isInView2]) {
    
    
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"alert"
                                                                message:@"are you sure you want to exit?"
                                                               delegate:self
                                                      cancelButtonTitle:@"Cancel"
                                                      otherButtonTitles:@"Yes",nil];
            [alertView show];
    
            return NO;
            }
        }
        return YES;
    
    }
    
    
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
    
        if(buttonIndex == 1)
        {//ok button pressed
    
    
    
    
    //        NSInteger destinationTabIdx = 2;
    //        UIView * fromView = tabBarController.selectedViewController.view;
    //        UIView * toView = [[[[[tabBarController.viewControllers objectAtIndex:destinationTabIdx] navigationController] viewControllers] objectAtIndex:0] view];
    
            UINavigationController *nav = (UINavigationController *)_tabBarController.selectedViewController;
    
            NSLog(@"vc title: %@",nav.title);
    //        [UIView transitionFromView:fromView toView:toView duration:0.8
    //                           options: UIViewAnimationOptionTransitionNone
    //                        completion:^(BOOL finished) {
    //                            if (finished) {
    //                                tabBarController.selectedIndex = destinationTabIdx;
    //                            }
    //                        }];
            [nav popViewControllerAnimated:YES];
            [QuizViewController setInQuizViewController:NO];
            NSLog(@"app delegate transistion done");
    
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多