我有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
?
答案 0 :(得分:2)
当您展示自己的CategoryGroupViewController
时,默认情况下它不会显示导航栏。
您必须将CategoryGroupViewController
设置为rootViewController
UINavigationController
而不是呈现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];
}
答案 1 :(得分:1)
正如我上面提到的,如果你想要一个navigaioncontroller,你可以将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];
}
答案 2 :(得分:0)
在View加载之前,您需要实例化一个rootViewController [ALINK] https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ViewLoadingandUnloading/ViewLoadingandUnloading.html
答案 3 :(得分:0)
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对如何使用类有很好的解释。花几分钟阅读它将在未来为您带来巨大的帮助。