【问题标题】:UINavigation with UITabbar not working带有 UITabbar 的 UINavigation 不起作用
【发布时间】:2014-03-26 11:29:57
【问题描述】:

我正在尝试更改我正在为其编辑代码的应用程序中的视图。但是在呈现新的视图控制器时,导航栏似乎没有在特定情况下更新。现在行为看起来像这样: 我通过单击选项卡栏中的选项卡导航到 tableviewcontroller, 然后我使用以下 tableviewcontroller 导航到 viewcontroller:

 settingsController = [SettingsController create];
 [self.navigationController pushViewController: settingsController animated: YES];

我相信它称之为代码:

+(id)create
{
    SettingsController*settings = [[SettingsController alloc] init];
    NSBundle*bundle = [NSBundle bundleForClass: [SettingsController class]];
    [bundle loadNibNamed: @"SettingsController" owner: settings options: NULL];
    settings.view.frame = CGRectMake(0, 0, 320, 411);

    settings.title = @"Settings";

    return settings;
}

然后我从那里导航到另一个视图控制器:

SearchViewController*searchView;
searchView = [[SearchViewController alloc] initWithNibName:@"SearchView" bundle: [NSBundle mainBundle]];
    [self presentViewController:searchView animated:YES completion:nil];

这就是行为开始出现错误的地方,导航栏不会更新到视图控制器中的更改。这段代码不是我写的,但是清理它让我头疼。

【问题讨论】:

    标签: ios uitabbar


    【解决方案1】:

    如果你使用的是navigationController,那么你不应该调用

    [self presentViewController:searchView animated:YES completion:nil];

    你应该使用

    [self.navigationController pushViewController:searchView animated:YES];

    【讨论】:

      【解决方案2】:

      另外,最好使用标准的内置函数来初始化一个新的视图控制器。

      SettingsController*settings = [[SettingsController alloc] initWithNibName:@"SettingsController" bundle:nil];
      [self.navigationController pushViewController:settings animated:YES];
      

      然后使用视图控制器中的默认方法。

      - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
      {
      self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
      if (self) {
          self.title = @"Settings";
          //other view changes.
      }
       return self;
      }
      

      【讨论】:

        【解决方案3】:

        使用导航控制器初始化tabbarcontroller的方法见这个例子

        - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
        { 
        UIViewController *viewController1, *viewController2,*viewController3;
        viewController1 = [[ViewController alloc] init];
        viewController2 = [[FormStatusViewController alloc] initWithNibName:@"FormStatusViewController" bundle:nil];
        viewController3 = [[DocumentsViewController alloc] init];
        
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController1];
        UINavigationController *nav1 =[[UINavigationController alloc] initWithRootViewController:viewController2];
        UINavigationController *nav2 =[[UINavigationController alloc] initWithRootViewController:viewController3];
        
        
        nav.navigationBarHidden=YES;
        nav1.navigationBarHidden=YES;
        nav2.navigationBarHidden=YES;
        
        
        NSArray *viewsArray = [[NSArray alloc] initWithObjects:nav,nav1,nav2, nil];
        self.formTabBar= [[UITabBarController alloc] init];
        [self.formTabBar setViewControllers:viewsArray];
        
        UITabBar *tabBar = self.formTabBar.tabBar;
        UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
        UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
        UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
        
        
        tabBarItem1.title = @"FORM";
        tabBarItem2.title = @"STATUS";
        tabBarItem3.title = @"DOCUMENTS";
        
        UIImage *tabBackground = [[UIImage imageNamed:@"tab_bg.png"]
                                  resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        [[UITabBar appearance] setBackgroundImage:tabBackground];
        
        if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7) {
            [tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"form.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"form_h.png"]];
            [tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:@"status.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"status_h.png"]];
            [tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:@"documents.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"documents_h.png"]];
        } else {
            tabBarItem1.selectedImage = [[UIImage imageNamed:@"form_h.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
            tabBarItem1.image = [[UIImage imageNamed:@"form.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
        
            tabBarItem2.selectedImage = [[UIImage imageNamed:@"status_h.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
            tabBarItem2.image = [[UIImage imageNamed:@"status.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
        
            tabBarItem3.selectedImage = [[UIImage imageNamed:@"documents_h.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
            tabBarItem3.image = [[UIImage imageNamed:@"documents.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
        
        
        }
        [[UITabBar appearance] setSelectionIndicatorImage:
         [UIImage imageNamed:@"tab_select_indicator.png"]];
        
        [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                           [UIColor whiteColor], UITextAttributeTextColor,
                                                           nil] forState:UIControlStateNormal];
        
        
        [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                           [UIColor whiteColor], UITextAttributeTextColor,
                                                           nil] forState:UIControlStateHighlighted];
        
        
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.rootViewController = self.formTabBar;
        [self.window makeKeyAndVisible];
        
        return YES;
        }
        

        现在在您的视图控制器中假设您想要导航到该选项卡中的另一个视图控制器的第一个选项卡中的 firstviewcontroller,然后使用下面的代码

        SearchViewcontroller *searchView =[[SearchViewcontroller alloc]init];
        [self.navigationController pushViewController:searchView animated:YES];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-02-02
          • 1970-01-01
          • 2010-11-08
          • 2012-07-20
          • 1970-01-01
          相关资源
          最近更新 更多