ios - 调用和显示 NSString

标签 ios nsstring nslog

你好,我相信你们都能回答这个问题,但这让我很烦恼,因为我很愚蠢。

我有一个数组,可以存储 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/

相关文章:

ios - merge git 分支缺陷后 Xcode 4 中 "No Scheme"问题的最佳解决方案

iphone - 不允许分配给具有只读属性的属性

ios - UITableView 复选标记有白色背景

ios - 具有 NSLinkAttribute 的属性字符串不会在 iPhone 上呈现

objective-c - Objective C 覆盖自定义对象的 %@

iphone - 将图像命名为导航栏名称

iphone - NSString 中子字符串之后的子字符串

ios - Realm 不支持 NSDiacriticInsensitivePredicateOption

xcode - NSLog 给我发出不可纠正的警告

ios - NSLog 不会从 ViewController 类输出,但会从 AppDelegate 类输出。 iOS :Objective C