Jacek
Jacek

Reputation: 1914

iPhone. UITableView and pushing view controller

I have a working UITableView in my view controller. It is being successfully populated 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

Upvotes: 2

Views: 3805

Answers (4)

Plamen Dragozov
Plamen Dragozov

Reputation: 314

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

Upvotes: 1

Jacek
Jacek

Reputation: 1914

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.

Upvotes: 0

bddckr
bddckr

Reputation: 1392

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.

Upvotes: 3

Warrior
Warrior

Reputation: 39374

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

Upvotes: 0

Related Questions