ios - Tableview显示警报错误

标签 ios objective-c uialertview

我正在编写一个简单的 tableview 应用程序,当点击该行时,会出现一个警告。

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *rowValue =self.greekLetters[indexPath.row];
   // NSString *message=[[NSString alloc] initWithFormat:@"You selected%@!",rowValue];

   if (indexPath.row==0) {
        NSString *message=[[NSString alloc] initWithFormat:@"This is course aaaaa",rowValue];
    }

    if (indexPath.row==1) {
        NSString *message=[[NSString alloc] initWithFormat:@"This is course aaaaa"];
    }


    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Course introduction" message:message delegate:nil cancelButtonTitle:@"Return to courses list" otherButtonTitles:nil, nil];

    [alert show];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

但是,Xcode 不喜欢我这里的 if 语句,而下面的语句有效。

NSString *message=[[NSString alloc] initWithFormat:@"You selected%@!",rowValue];

谁能给我一个如何使用这里消息的例子吗?

最佳答案

这是作用域的问题。您需要在 if 语句之前声明消息:

NSString *message;

if (indexPath.row==0) {
    message=[[NSString alloc] initWithFormat:@"This is course aaaaa",rowValue];
}

else if (indexPath.row==1) {
    message=[[NSString alloc] initWithFormat:@"This is course aaaaa"];
}

UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Course introduction" message:message delegate:nil cancelButtonTitle:@"Return to courses list" otherButtonTitles:nil];

[alert show];
[tableView deselectRowAtIndexPath:indexPath animated:YES];

但是,如果类(class)编号由行号确定:

NSString *rowValue =self.greekLetters[indexPath.row];

NSString *message = [[NSString alloc] initWithFormat:@"This is course %@",rowValue];

UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Course introduction" message:message delegate:nil cancelButtonTitle:@"Return to courses list" otherButtonTitles:nil];

[alert show];
[tableView deselectRowAtIndexPath:indexPath animated:YES];

关于ios - Tableview显示警报错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27882063/

相关文章:

ios - 在 UIViewController 之间传递数据

objective-c - 两个 UITableView 单元格/部分之间的黑色边框

ios - ios-应该在NSUrlConnection的返回 block 中使用UIAlertView吗?

ios - 每次从 SwiftUI 中的详细 View 返回时,如何阻止 View 刷新?

iphone - 核心数据 NSFetchRequest 在删除对象和重新获取数据后返回未排序的数组

ios - 为什么我在正确的时区得到错误的时间输出?

ios - 如何启用 UIButton?

objective-c - UIAlertController 延迟显示

ios - 未调用 alertViewShouldEnableFirstOtherButton

iOS QLPreviewController 显示 pdf 保存到文件系统