PDA

View Full Version : Unsure of path for pushViewController




Inkreaser
Nov 30, 2009, 07:27 AM
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:


-(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:


// 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
Nov 30, 2009, 10:27 AM
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, [self.navigationController pushViewController:viewController animated:YES];

Inkreaser
Nov 30, 2009, 02:03 PM
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
Nov 30, 2009, 02:47 PM
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
Nov 30, 2009, 03:12 PM
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:


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
Nov 30, 2009, 03:23 PM
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
Nov 30, 2009, 03:50 PM
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:


-(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];
}
}