1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Unsure of path for pushViewController

Discussion in 'iPhone/iPad Programming' started by Inkreaser, Nov 30, 2009.

  1. macrumors newbie

    #1
    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.
     
  2. Moderator

    dejo

    Staff Member

    #2
    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];
     
  3. macrumors newbie

    #3
    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!
     
  4. Moderator

    dejo

    Staff Member

    #4
    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.
     
  5. macrumors newbie

    #5
    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?
     
  6. Moderator

    dejo

    Staff Member

    #6
    Your suspicion is correct; you have the syntax wrong.

    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.
     
  7. macrumors newbie

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

Share This Page