这段代码有什么问题吗?

时间:2010-04-06 01:30:46

标签: iphone

我正在使用它来确定从UITableView作为输入的结果转到下一个视图。以下代码无效,但我认为应该是!

你觉得它有什么问题吗?

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

}

每个项目/对象定义如下:

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

4 个答案:

答案 0 :(得分:2)

您正在发布类对象,而不是您的clases实例:这是一个非常糟糕的主意(您想要[nTItemsController release])。其次,您要按内存地址比较字符串:您很可能正在寻找-isEqual:-isEqualToString:方法。除此之外,它看起来还不错。

(我希望你的代码在实际文件中缩进/格式化更好...通常你想缩进而不是在块中最后一行的末尾结束花括号......等等。) p>

答案 1 :(得分:0)

使用“isEqualToString”来比较NSString,并且“release”方法消息应该发送给一个实例而不是类对象。

答案 2 :(得分:0)

试试这个:

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

}

答案 3 :(得分:0)

如果menuArray包含字典,您可以像这样在当前选项中找到标题键:

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

顺便说一下,“收据”在“重印收据”中拼写错误。