Reputation: 371
I am writing a simple tableview app, when tapping the row, a alert shows up.
(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];
However, Xcode doesn't like my if statement here, while the following statement works.
NSString *message=[[NSString alloc] initWithFormat:@"You selected%@!",rowValue];
Could anyone give me an example of how to use the message here please?
Upvotes: 0
Views: 263
Reputation: 2881
This is a problem with scope. You need to declare message before the if statement:
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];
However, if the course number is determined by the row number do:
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];
Upvotes: 1
Reputation: 7256
You've forgotten to include %@
in the first if-statement.
NSString *message= [[NSString alloc] initWithFormat:@"This is course aaaaa %@",rowValue];
By the way, there's no need to do [[NSString alloc] init]
at all, just use [NSString stringWithFormat:...]
Upvotes: 0