使用标签栏导航无法正常工作

时间:2014-03-26 01:00:39

标签: ios uitabbar

我正在尝试更改我正在编辑代码的应用中的视图。但是在呈现新的viewcontroller时,导航栏似乎没有在特定实例中更新。现在行为看起来像这样: 我通过单击tabbar中的选项卡导航到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];

这是行为开始出错的地方,导航栏不会更新视图控制器中的更改。我没有编写这段代码,但它一直让我头疼,试图清理它。

3 个答案:

答案 0 :(得分:0)

如果您使用的是navigationController,则不应调用

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

你应该使用

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

答案 1 :(得分:0)

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

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;
}

答案 2 :(得分:0)

有关使用导航控制器初始化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];