【问题标题】:UINavigationController not appearing?UINavigationController 没有出现?
【发布时间】:2015-01-19 05:02:00
【问题描述】:

我有一个Add View,当你点击Category时,会执行以下代码

- (void)categoryTapped {
    CategoryGroupViewController *categoryGroupViewController = [[CategoryGroupViewController alloc] initWithNibName:@"CategoryGroupViewController" bundle:nil];
    [self presentViewController:categoryGroupViewController animated:YES completion:nil];
}

CategoryGroupViewController.h 看起来像

@interface CategoryGroupViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UINavigationController *navigationController;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@end

CategoryGroupViewController.m 看起来像

#import "CategoryGroupViewController.h"
#import "Helper.h"

static NSString *CellIdentifier = @"Cell";

@interface CategoryGroupViewController ()
@property(nonatomic, strong) NSArray *categories;
@end

@implementation CategoryGroupViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // (todo) should come from API
        self.categories = @[@"Food & Drink", @"Utilities"];
    }
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifier];
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSLog(@"categoryGroup View loaded");
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:self];
    self.navigationController.title = @"Pick Category";
}
...
}

当我运行我的应用程序时,我在日志中看到以下内容

2014-11-20 21:29:53.589 myapp-ios[30332:70b] categoryGroup View loaded

但在模拟器上,我看到了

为什么我看不到NavigationController

【问题讨论】:

  • 您在使用情节提要吗?导航控制器可以从那里完全“关闭”..
  • 我认为你只展示了CategoryGroupViewController,它不是导航控制器,所以你想展示一个视图控制器,将它设置为导航控制器的根视图控制器
  • 检查storyboard中是否有导航控制器。

标签: ios iphone ios7 uinavigationcontroller


【解决方案1】:

当您展示您的 CategoryGroupViewController 时,默认情况下它不会显示导航栏。

对于UINavigationController,您必须将CategoryGroupViewController 设置为rootViewController,而不是呈现CategoryGroupViewController 呈现新创建的UINavigationController

- (void)categoryTapped{
    CategoryGroupViewController *categoryGroupVC = [[CategoryGroupViewController alloc] initWithNibName:@"CategoryGroupViewController"  bundle:nil];      
   UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController: categoryGroupVC]; 
   [self presentViewController:categoryGroupViewController animated:YES completion:nil];
}

【讨论】:

    【解决方案2】:

    如上所述,如果您想要一个导航控制器,您可以将CategoryGroupViewController 设置为导航控制器的根视图并显示它,例如,

    - (void)categoryTapped
    {
        CategoryGroupViewController *categoryGroupViewController = [[CategoryGroupViewController alloc] initWithNibName:@"CategoryGroupViewController"  bundle:nil];
        //add this        
       UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController: categoryGroupViewController]; 
       //present the navigation controller which contains root view controller categoryGroupViewController 
       [self presentViewController:navController animated:YES completion:nil];
    }
    

    【讨论】:

    • ` *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“应用程序试图以模态方式呈现活动控制器 <0xb561410>
    【解决方案3】:
    【解决方案4】:

    UIViewController 已经是navigationController property,所以这是多余的:

    // Remove this property from CategoryGroupViewController
    @property (nonatomic, strong) UINavigationController *navigationController;
    

    以您尝试的方式构建用户界面也是不正确的,并且会导致 CategoryGroupViewController 和 UINavigationController 之间的循环引用。首先你需要像这样修改你的-viewDidLoad 方法:

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
        NSLog(@"categoryGroup View loaded");
    
        // UINavigationController will use this automatically. 
        self.title = @"Pick Category";    
    }
    

    那么你应该改变你在原始方法中预设视图控制器的方式:

    - (void)categoryTapped {
        CategoryGroupViewController *categoryGroupViewController = [[CategoryGroupViewController alloc] initWithNibName:@"CategoryGroupViewController" bundle:nil];
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:categoryGroupViewController];
        [self presentViewController:navController animated:YES completion:nil];
    }
    

    最后,introduction in the UINavigationController documentation 很好地解释了如何使用该类。花几分钟时间阅读它会对您将来有很大帮助。

    【讨论】:

      猜你喜欢
      • 2015-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多