Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Inkreaser

macrumors newbie
Original poster
Nov 25, 2009
11
0
Hi,

I have a UITableView which has 2 rows in it, and I want to use pushViewController for both rows, but I want to push a different view in depending on what row was selected.

I want to do this by specifying a method based on the row selected, like so:

Code:
-(void)pushViewControllerFirstView:(id)sender {
	FirstView *firstView = [[FirstView alloc] initWithNibName:@"FirstView" bundle:nil];
	[self.navigationController pushViewController:firstView animated:YES];
	[firstView release];
}

-(void)pushViewControllerSecondView:(id)sender {
	SecondView *secondView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];
	[self.navigationController pushViewController:secondView animated:YES];
	[secondView release];
}

// This is inside didSelectRowAtIndexPath...
if (indexPath.row == 0){
	[self.parentViewController pushViewControllerFirstView];
} else if (indexPath.row == 1){
	[self.parentViewController pushViewControllerSecondView];
}

But it doesn't work. It says that "'UIViewController' may not respond to '-pushViewControllerFirstView'"

However, if I remove the methods and insert the code manually, like this, it works:

Code:
// This is inside didSelectRowAtIndexPath...
if (indexPath.row == 0){
	FirstView *firstView = [[FirstView alloc] initWithNibName:@"FirstView" bundle:nil];
	[self.navigationController pushViewController:firstView animated:YES];
	[firstView release];
} else if (indexPath.row == 1){
	SecondView *secondView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];
	[self.navigationController pushViewController:secondView animated:YES];
	[secondView release];
}

So I believe it's just the path for what I'm telling to receive the methods that isn't right? Can anyone help? I know the second way works but I think it might be better practice to do it the first way. Thanks in advance.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
You may want to include the actual code you are using since I don't see where you are actually calling pushViewController:animated: anywhere. As in,
Code:
[self.navigationController [B]pushViewController:[/B]viewController animated:YES];
 

Inkreaser

macrumors newbie
Original poster
Nov 25, 2009
11
0
Sorry Dejo, I edited my code for the purpose of this post and must've missed it out, I've edited my original post now, hope you can help!
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
But it doesn't work. It says that "'UIViewController' may not respond to '-pushViewControllerFirstView'"
That's because you aren't passing a parameter (id) like you have setup in the definition of your methods. If you're not sure what I'm talking about here, it's time to step away from the real coding and go back and (re)learn the basics of Objective-C messaging.
 

Inkreaser

macrumors newbie
Original poster
Nov 25, 2009
11
0
I am very new to Objective-C and the iPhone SDK but I'm trying to learn as I go, I do understand what you're saying, and I did already try that, but it didn't work either, though maybe I have the syntax wrong? I tried this:

Code:
if (indexPath.row == 0){
	[self.parentViewController pushViewControllerFirstView:(id)sender];
} else if (indexPath.row == 1){
	[self.parentViewController pushViewControllerSecondView:(id)sender];
}

Is that right, and do you recommend removing the reference to id as it's not an IBAction?
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
though maybe I have the syntax wrong?
Your suspicion is correct; you have the syntax wrong.

...and do you recommend removing the reference to id as it's not an IBAction?
You can probably remove the id parameter in this case, but it depends if you plan on needing a reference to the sender at some point.

Again, step back from the real coding and go learn the basics. Otherwise, you are going to just keep running into issue after issue.
 

Inkreaser

macrumors newbie
Original poster
Nov 25, 2009
11
0
Thanks Dejo. I'll keep the id reference for now as it's not doing any harm. I figured out the solution and I realise what I was doing wrong. I'm still learning the basics but throw myself in the deep end for a while every now and then to try out some ideas. Thanks for your advice.

For anyone needing the solution, here's what I did:

Code:
-(void)pushViewControllerFirstView:(id)sender {
	FirstView *firstView = [[FirstView alloc] initWithNibName:@"FirstView" bundle:nil];
	[self.navigationController pushViewController:firstView animated:YES];
	[firstView release];
}

-(void)pushViewControllerSecondView:(id)sender {
	SecondView *secondView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];
	[self.navigationController pushViewController:secondView animated:YES];
	[secondView release];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	//NSLog(@"%i", indexPath.row);
	if (indexPath.row == 0){
		[self pushViewControllerFirstView:self];
	} else if (indexPath.row == 1){
		[self pushViewControllerSecondView:self];
	}
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.