开发者

iPhone. UITableView and pushing view controller

开发者 https://www.devze.com 2022-12-22 17:37 出处:网络
I have a working UITableView in my view controller. It is being successfully po开发者_Go百科pulated and seems to be fine. However, when I try using following function, new view is not loaded (function

I have a working UITableView in my view controller. It is being successfully po开发者_Go百科pulated and seems to be fine. However, when I try using following function, new view is not loaded (function is called, I get output from NSLog):

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"asf");
    [self.navigationController pushViewController:sendRequestFavoriteController animated:YES];
}

What might be a problem? I get no compilation or debugging errors/warnings.

EDIT: I have tried allocating and initializing a view controller manually. I believe Plamen is right, because self.navigationController is nil. However, I have not yet succeeded.

EDIT2: I use [self.navigationController pushViewController:.. function successfully in the rest of the application. That's the only exception. navigationController is nil when i have UITableView. Why is that? What to do?

alt text http://files.droplr.com/files/11625842/AUk9W.Screen%20shot%202010-03-14%20at%2009.56.42.png


You should use an UINavigationController for this. That's your problem. And you need to initialize sendRequestFavoriteController. There's more to do than declaring it as a property. This will not just "magically create" this object for you. You'd normally create this object before pushing it onto the stack like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"asf");

    SendRequestFavoriteController *aController = [[SendRequestFavoriteController alloc] initWithNibName:@"theNameOfTheNib" bundle:nil];
    self.sendRequestFavoriteController = aController;
    [aController release];

    [self.navigationController pushViewController:sendRequestFavoriteController animated:YES];
}

But I don't see the need to declare sendRequestFavoriteController as an ivar. This way you'd do this (and get rid of your property and ivar):

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"asf");

    SendRequestFavoriteController *aController = [[SendRequestFavoriteController alloc] initWithNibName:@"theNameOfTheNib" bundle:nil];
    [self.navigationController pushViewController:sendRequestFavoriteController animated:YES];
    [aController release];
}

To use a UINavigationController you'll need to replace your current UIViewController with a UINavigationController which has this UIViewController inside of it.
Have a look at the View Controller Programming Guide by Apple.
Edit: By "current" I mean the selected tab "history". Or whatever view controller you're talking about.


Is your table view in an UINavigationController stack at all? The navigationController property will be nil otherwise (and the method call will do nothing).


Try this

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"asf");

  sendRequestFavouriteController *sendRequest=[[sendRequestFavouriteController alloc]initWithNibName:@"sendRequestFavouriteController" bundle:[NSBundle mainBundle]];

[self.navigationController pushViewController:sendRequest animated:YES];
[sendRequest release];

}

All the Best


ChriB is right. Here's his comment:

Are you sure you're using a UINavigationController for the "history" tab there? And inside of this UINavigationController your UIViewController? – ChriB

The thing is I should have add UINavigationController in the main view (in Interface Builder), then UIViewController inside. My mistake was to add UIViewController directly.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号