iphone - IBAction 中格式错误的 NSstring

标签 iphone objective-c ios

我已经在 ViewdidLoad 中用一些字符串值声明了 NSString,比如..

int i=1;
strval=[NSString stringWithFormat:@"%03d",i];
strval=[NSString stringWithFormat:@"S%@",strval];

NSLog(@"Value %@",strval);

它给出了 S001 的正确结果,但是当我在 IBAction 中打印相同的结果时,

- (IBAction)stringvalue:(id)sender {
NSLog(@"Value %@",strval);
}

它每次都给出未知值。有时它会抛出 EXEC_BAD_ACCESS 错误。

请帮帮我..

最佳答案

尝试这样的事情

在.h 中

  @property (nonatomic, strong) NSString *strval;

.m

  @synthesize strval = _strval

  - (void)viewDidLoad 
  {
      int i = 4;
      // ARC
      _strval = [NSString stringWithFormat:@"hello %d", i];
      // None ARC
      // strcal = [[NSString alloc] initwithFormat:@"hello %d",i];
      NSLog(@"%@", _strval);
      // Prints "hello 4" in console (TESTED)
  } 

  - (IBAction)buttonPress:(id)sender
  {
      NSLog(@"%@", _strval);
      // Prints "hello 4" in console (TESTED)
  }

使用 ARC。这已经过测试,并且按照问题提出的方式工作。

关于iphone - IBAction 中格式错误的 NSstring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13234264/

相关文章:

ios - Swift 2.0 按属性对对象数组进行排序

iphone - 您可以在iPad上进行的OpenGL ES调用数量上限吗?

iphone - map View 崩溃时的 GDirections 路线

iphone - 在重大位置更改时重新加载/刷新 View Controller (来自应用程序委托(delegate))

iphone - 使用什么框架从 iPhone 连接到 SQL Server 实例?

ios - 透明背景 UIView drawRect 圆形

iphone - UILabel 文本错误

objective-c - UIView retainCount为0,但内存没有释放

ios - UICollectionview 和单元格

ios - Xcode 10 构建错误 : 'must declare at least one output file'