views:

167

answers:

4

Hello.

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

A: 

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

Warrior
The same thing :/
Jacek
A: 

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

Plamen Dragozov
You're right. self.navigationController is nil . How should I fix it? BTW I used following code to check it:- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"asf"); if (self.navigationController == nil){ NSLog(@"Nil!!"); }}
Jacek
You need to create an UINavigationController stack and push your table view controller to that stack. That is what shows the bar and handles going back and forth.Read the UINavigationController documentation from the link in my answer. It explains it very well.
Plamen Dragozov
+2  A: 

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.

bddckr
Still, does not work.
Jacek
You should edit your question and tell us what you have tried.
bddckr
A: 

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.

Jacek