UINavigationController没有出现?

时间:2014-11-21 05:35:48

标签: ios iphone ios7 uinavigationcontroller

我有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

但在模拟器上,我看到了 enter image description here

为什么我看不到NavigationController

4 个答案:

答案 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)

答案 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对如何使用类有很好的解释。花几分钟阅读它将在未来为您带来巨大的帮助。