【问题标题】:Navigation with xib in iOS app在 iOS 应用中使用 xib 进行导航
【发布时间】:2013-10-28 03:13:27
【问题描述】:

请帮助理解导航。我正在使用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. 我应该在哪里创建下一个视图?在我的代码中,视图已在“上一个”类中创建:在 FirstViewController 中创建的 view2,在 SecondViewController 中创建的 view3 等。所有新的 ViewController 都在启动导航的方法中,是不是正确的方法?我认为这是错误的,但导航工作正常。

  2. 导航栏中的标题问题。原来 view2 的标题只有在从 view1 移动到 view2 时才会显示,但是当从 view3 回到 view2 时 - 标题消失了。我用谷歌搜索,尝试将 self.title = @"name" 添加到 viewDidLoadinitWithNibNameviewWillAppear - 这些都不起作用。

【问题讨论】:

  • 回答第1点。我会说这是正确的。第 2 点。根据您在此处粘贴的代码,它应该可以按预期工作。您正在使用导航控制器中的内置后退按钮吗?
  • 是的,我不使用自定义导航项或导航栏。其余的代码是默认的,什么都不做。也许原因是我应该在界面构建器中创建导航栏,创建插座,属性等?
  • 不,如果你不想的话,你不需要在界面生成器中创建它。我真的找不到您发布的内容有什么问题。

标签: objective-c navigation xib xcode5 viewcontroller


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 1970-01-01
    • 2015-01-05
    • 2010-11-26
    • 2016-04-15
    • 2020-10-23
    • 1970-01-01
    相关资源
    最近更新 更多