你好,我相信你们都能回答这个问题,但这让我很烦恼,因为我很愚蠢。
我有一个数组,可以存储 didSelectRowAtIndexPath
行,然后存储 NSLog formatSelected。然后,我弹出 View Controller 并将 formatSelected 显示为按钮标题。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *formatSelected = [[NSString alloc]init];
formatSelected:[format objectAtIndex:indexPath.row];
NSLog(@"this is the format selected %@",formatSelected);
[button2 setTitle:[format objectAtIndex:indexPath.row] forState:UIControlStateNormal];
[navigationControler popViewControllerAnimated:YES];
}
效果很好。
我的问题是,在上一个 View 中,新标题的按钮是我有另一个按钮和一个标签。
我希望能够按下第二个按钮并在标签中显示 formatSelected 字符串或 NSLog 它
-(IBAction)printResults{
NSString *fmat = [[NSString alloc]initWithFormat:@"%@",formatSelected];
NSLog(@"%@",fmat);
NSLog(@"nslong button pressed");}
但是 NSLog 只显示(null)?
我有@property(nonatomic,retain)NSString *formatSelected;
并合成了它。
我做错了什么?
最佳答案
您正在 tableView:didSelectRowAtIndexPath:
方法中将 formatSelected
声明为局部变量。在该方法退出后,您在此方法中分配给 formatSelected
的任何内容都将不再可访问。您正在将选定的格式分配给该局部变量,而不是分配给您的属性(及其相应的实例变量)。
使用 [self setFormatSelected:[format objectAtIndex:indexPath.row]];
并完全删除 NSString *formatSelected...
行。
关于ios - 调用和显示 NSString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11254244/