2013-10-19 44 views
0

请帮助了解导航。我正在与xibs合作。该计划是:https://www.dropbox.com/s/o82fxpte0hmyxcq/Scheme_Simple.jpg。 这里是我的代码:在iOS应用程序中使用xib导航应用程序

@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. 我应该在哪里创建下一个看法?在我的代码视图中创建了“前一个”类:在FirstViewController中创建的view2,在SecondViewController中创建的view3等。所有新的ViewController都在启动导航的方法中,是正确的方式吗?我认为这是错误的,但导航正在工作。

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

+0

要回答点1.我会说这是正确的。 要点2.从您粘贴的代码中,它应该按预期工作。您正在使用导航控制器中的内置后退按钮? –

+0

是的,我不使用自定义导航项目或导航栏。其余的代码是默认的,什么都不做。也许原因是我应该在界面构建器中创建导航栏,创建出口,属性等? –

+0

不,您不需要在界面构建器中创建它,如果您不想。我真的无法找到你发布的任何内容。 –

回答

0

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

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