0

当我实现navigationController时,我遇到了一个非常奇怪的行为。我有一个带有 UITableView 的 LogInViewController,其中的单元格填充如下:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == 0) {
        return cellUsername;
    }
    if (indexPath.row == 1){
        return cellPassword;
    }
    else{
        return nil;
    }
}

cellUsername 和 cellPassword 是我在 .xib 上创建的单元格。当我将此 LogInViewController 作为 rootViewController 添加到我的 appDelegate 中时,它工作正常!我想实现一个navigationController,所以我用navController @property创建了RootViewController,在我的appDelagate中我这样称呼它

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];


RootViewController *rootView = [[RootViewController alloc]init];
rootView.navController = [[UINavigationController alloc] initWithRootViewController:rootView];

self.window.rootViewController = rootView.navController;
[self.window makeKeyAndVisible];
return YES;

在 viewDidLoad 上的 RootViewController 中,我有以下代码:

[super viewDidLoad];
self.navController = [[UINavigationController alloc]init];
LogInViewController *loginView = [[LogInViewController alloc]init];
[[self navigationController] pushViewController:loginView animated:YES];

It opens my LogInViewController,but only the first cell,cellUsername is added to my UiTableView, what is wrong? Am still a beginner,so I might be missing something stupid probably.. but it works wihout navigationController,so am pretty confused.. Thank you!

4

2 回答 2

0

in AppDelegate file

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];


RootViewController *rootView = [[RootViewController alloc]init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootView];

self.window.rootViewController =  navController;
[self.window makeKeyAndVisible];
return YES;

Further there is no need to create another navigationController in the viewDidLoad of the RootViewController class

于 2012-11-07T13:31:47.537 回答
0

I'm not sure if this has anything to do with your problem, but you shouldn't be using this line in your viewDidLoad:

self.navController = [[UINavigationController alloc]init];

Your RootViewController is already embedded in a navigation controller, so you can just use self.navigationController to get a reference to it.

I'm also not sure about this code, it may be ok, but it's not the way it's usually done:

RootViewController *rootView = [[RootViewController alloc]init];
rootView.navController = [[UINavigationController alloc] initWithRootViewController:rootView];
self.window.rootViewController = rootView.navController;

The usual way to do it is like this:

RootViewController *rootView = [[RootViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rootView];
self.window.rootViewController = nav;
于 2012-11-07T16:30:23.423 回答