2014-11-21 31 views
1

我在那裏當你在Category挖掘,下面的代碼被執行UINavigationController不出現?

- (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"; 
} 
... 
} 

當我運行我的應用程序Add View ,我在日誌中看到以下內容

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

但在模擬器,我看到
enter image description here

爲什麼我看不到NavigationController

+0

你在使用故事板嗎?導航控制器可以從那裏完全'關閉'.. – 2014-11-21 05:42:54

+0

我認爲你只呈現'CategoryGroupViewController'它不是一個導航控制器,所以你想呈現一個視圖控制器將其設置爲導航控制器的根視圖控制器 – 2014-11-21 06:38:53

+0

檢查是否在故事板中有導航控制器。 – 2014-11-21 07:57:33

回答

2

當您在展示您的CategoryGroupViewController時,默認情況下它不會顯示導航欄。

你必須設置你的CategoryGroupViewControllerrootViewControllerUINavigationController和,而不是呈現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

正如我上面提到如果u想要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]; 
} 
+0

'***由於未捕獲的異常'NSInvalidArgumentException'而終止應用程序,原因:'應用程序試圖以模態方式呈現活動控制器。' – daydreamer 2014-11-22 22:23:20

+0

抱歉,延遲..:(我編輯的代碼,因爲我評論你必須提出'navController'不'categoryGroupViewController'.. – 2014-11-25 04:53:58

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";  
} 

那麼你應該改變你去你的原始方法preseting視圖控制器的方式:

- (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有很大的應該如何使用該類的解釋。花幾分鐘時間閱讀它將在未來幫助你。

相關問題