在iOS应用中使用xib进行导航

时间:2013-10-19 07:59:50

标签: objective-c navigation xib xcode5 viewcontroller

请帮助理解导航。我正在使用xibs。该计划是:https://www.dropbox.com/s/o82fxpte0hmyxcq/Scheme_Simple.jpg。 这是我的代码:

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    FirstViewController *firstViewController = [[firstViewController alloc] initWithNibName:@"firstViewController" bundle:nil];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
    self.window.rootViewController = navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

@implementation FirstViewController
- (IBAction)button1Tapped:(id)sender {
    SecondViewController *secondViewController = [[SecondViewController alloc] init];
    secondViewController.title = @"View2";
    [self.navigationController pushViewController:secondViewController animated:YES];
}

@implementation SecondViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ThirdViewController *thirdViewController = [[ThirdViewController alloc] init];
    thirdViewController.title = @"View3";

    if (indexPath.row == 0) {    
        [self.navigationController pushViewController:thirdViewController animated:YES];
        [secondViewTableView deselectRowAtIndexPath:indexPath animated:YES];
    }
}

所以,我有疑问:

  1. 我应该在哪里创建下一个视图?在我的代码视图中创建了“previous”类:在FirstViewController中创建的view2,在SecondViewController等中创建的view3。所有新的ViewControllers都在启动导航的方法内,是否正确?我认为这是错误的,但导航工作正常。

  2. 导航栏中标题的问题。事实证明,view2的标题仅在从view1移动到view2时显示,但是当从view3返回到view2时 - 标题消失。我用Google搜索,尝试将self.title = @"name"添加到viewDidLoadinitWithNibNameviewWillAppear - 这些都不起作用。

1 个答案:

答案 0 :(得分:0)

所以我解决了导航栏标题消失的问题。问题出在我的自定义后退按钮中:self.navigationItem.title = @""; 它正在工作,标题“Back”从我的后退按钮消失了,但导航栏的标题也消失了。使按钮无标题的正确方法是:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:nil action:nil];
[self.navigationItem setBackBarButtonItem:backButton];