tags:

views:

154

answers:

4

I am using this to determine which view to go to next, from the result as input from UITableView. The following code isn't working, but I think it should be!

Do you see anything wrong with it?

NSString *option = [menuArray objectAtIndex:indexPath.row];


if (option == @"New Transaction"){

      NTItems *nTItemsController = [[NTItems alloc] initWithNibName:@"NTItems" bundle:nil]; 
      [self.navigationController pushViewController:nTItemsController animated:YES];
      [NTItems release];
} else if ([option isEqualToString:@"Previous Transactions"]){
} else if ([option isEqualToString:@"Reprint a reciept"]){
} else if ([option isEqualToString:@"Settings"]){
} else if ([option isEqualToString:@"Logout"]){
             LoginViewController *nTItemsController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];  
             [self.navigationController pushViewController:nTItemsController animated:YES];
             [nTIemsController release];    

}

Each item/object is defined as follows:

[menuArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                      NSLocalizedString(@"Logout", @"Logout"), @"title",
                      nil,
                      nil]];
+2  A: 

You are releasing a class object, not your instances of the clases: that's a really bad idea (you want [nTItemsController release]). Secondly, you are comparing a string by memory address: you are most likely looking for the -isEqual: or the -isEqualToString: method. Besides that, though, it looks fine.

(I hope that your code is indented/formatted better in the actual file...usually you want to indent and not end your curly braces on the end of the last line in the block...etc.)

chpwn
I get our favourite: "unrecognized selector sent to instance" on NSCFDictionary isEqualToString: now. Thanks for correcting the class object issue :)
Shamil
That means that `option` is not a string, but an `NSDictionary`. Does your `menuArray` contain dictionaries?
Ian Henry
Check amendement, I am using dictionaries.
Shamil
A: 

Using "isEqualToString" to compare the NSString, and the "release" method message should send to a instance but the class object.

I get our favourite: "unrecognized selector sent to instance" on NSCFDictionary isEqualToString: now. Thanks for correcting the class object issue :)
Shamil
A: 

try this:

NSString *option = [menuArray objectAtIndex:indexPath.row];

NSAssert([option isKindOfClass [NSString class]], @"Expecting an NSString here!");

if ([option isEqualToString:@"New Transaction"]){

      NTItems *nTItemsController = [[NTItems alloc] initWithNibName:@"NTItems" bundle:nil]; 
      [self.navigationController pushViewController:nTItemsController animated:YES];
      [nTItemsController release];
} else if ([option isEqualToString:@"Previous Transactions"]){
} else if ([option isEqualToString:@"Reprint a reciept"]){
} else if ([option isEqualToString:@"Settings"]){
} else if ([option isEqualToString:@"Logout"]){
             LoginViewController *nTItemsController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];  
             [self.navigationController pushViewController:nTItemsController animated:YES];
             [nTIemsController release];    

}
Cliff
A: 

If menuArray contains dictionaries, you can get to the title key in the current option like this:

NSDictionary *option = [menuArray objectAtIndex:indexPath.row];

NSString *optionTitle = [option objectForKey:@"title"];

if ([optionTitle isEqualToString:@"New Transaction"]) {     
    NTItems *nTItemsController = [[NTItems alloc] initWithNibName:@"NTItems" bundle:nil]; 
    [self.navigationController pushViewController:nTItemsController animated:YES];
    [nTItemsController release];
}
else if ([optionTitle isEqualToString:@"Previous Transactions"]) {
} 
else if ([optionTitle isEqualToString:@"Reprint a reciept"]) {
} 
else if ([optionTitle isEqualToString:@"Settings"]) {
} 
else if ([optionTitle isEqualToString:@"Logout"]) {
    LoginViewController *nTItemsController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
    [self.navigationController pushViewController:nTItemsController animated:YES];
    [nTItemsController release];    
}   

By the way, "receipt" is spelled wrong in "Reprint a reciept".

DyingCactus