Same XIB by touching different Object

Discussion in 'iOS Programming' started by Appleboom, Oct 7, 2009.

  1. Appleboom macrumors newbie

    Joined:
    Oct 7, 2009
    #1
    I'm busy with some kind of Catalog for Iphone.

    I want to show a list of Objects and when you touch one, you'll get extra information. Sounds easy, but it is driving me crazy. It doesn't matter which Object from the list I select, I always displays the SecondViewController.

    Here's the code:
    Rootviewcontroller.m:
    Code:
    #import "RootViewController.h"
    
    
    @implementation RootViewController
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
    	tableList = [[NSMutableArray alloc] init];
    	[tableList addObject:@"Object 1"];
    	[tableList addObject:@"Object 2"];
    
    	[self setTitle:@"Title for Nav bar"];
    
    - (void)viewDidUnload {
    	// Release anything that can be recreated in viewDidLoad or on demand.
    	// e.g. self.myOutlet = nil;
    }
    
    
    #pragma mark Table view methods
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    }
    
    
    // Customize the number of rows in the table view.
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [tableList count];
    }
    
    
    // Customize the appearance of table view cells.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
        static NSString *CellIdentifier = @"Cell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
        
    	cell.textLabel.text = [tableList objectAtIndex:indexPath.row];
    	
    	[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        return cell;
    }
    
    
    // Override to support row selection in the table view.
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    	if ([[tableList objectAtIndex:indexPath.row] isEqual:@"Object 1"]);
    	{
    		SecondViewController *second = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    			[second setTitle:@"Title of Object 1"];
    			[self.navigationController pushViewController:second animated:YES];
    	}
    	
    	if ([[tableList objectAtIndex:indexPath.row] isEqual:@"Object 2"]);
    	{
    		ThirdViewController *third = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
    		[third setTitle:@"Title of Object 2"];
    		[self.navigationController pushViewController:third animated:YES];
    	}
    		}
    	
    
    When I touch Object 1, I'll get a new screen with the Title of Object 2 and the View of Object 1.

    When I touch Object 2, I'll get the same.

    Does anybody know what mistake I'm making?
    Thanks in advance
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Both isEqual: calls are returning YES, so both if blocks are being executed. First, you need to determine why the isEqual: is succeeding in both cases. Second, I'm gonna suggest you use isEqualToString: instead. If you know both objects are strings, this is a faster way and the preferred approach. Third, I'm gonna suggest you code the second condition using an 'else if', so that it is only executed if the first condition fails.

    P.S. You are not setting your titles correctly. By setting them before pushing the view controller, you end up changing the label that appears in the Back button.
     
  3. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    depending, or not, on the amount of view controllers you have, you could refactor your code by simply moving all your view controllers into an array and push the one that is associated with the tableview index. alternatively, depending on the complexity of your views, you could use one view controller that would display an appropriate view according to the tableview's selection.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    UPDATE: Sorry, further investigation has caused me to discover something else wrong. It's not that the if conditions are both evaluating to YES, but the way they are coded. Tell me, Appleboom, if you see anything wrong with this?:
    Code:
    if (condition);
    {
        // code here
    }
     
  5. Appleboom thread starter macrumors newbie

    Joined:
    Oct 7, 2009
    #5
    I tried the things you guys said, but without succes. I will visualize the problem in the following image:
    [​IMG]

    So when I touch any Object (1-9) the navigation bar shows the title of the 2nd object and a back button to the first object.
    Object 1 is always displayed in the main screen. It doesn't matter which Object I touch.

    The activity on the three displayed screens is:

    Screen 1 --> Touch an object, doesn't matter which one
    Screen 2 --> Displays wrong combination of Objects (hit back button)
    Screen 3 --> Displays the good combination of Object 1 (but when touching Object 1 in main screen, screen 2 will pop-up)

    Besides, I'm quiet new to app development so please give me clear information.
    Thanks in Advance
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    You have an improper ';' at the end of your 'if (condition)' lines. This is the problem! Remove them and things should work better. Clear enough?
     
  7. Appleboom thread starter macrumors newbie

    Joined:
    Oct 7, 2009
    #7
    Thanks dejo! That did the trick. Everything works perfect now!:D
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    You're welcome. Do you understand why those superfluous ;'s were causing problems?
     

Share This Page