NSstring,IBAction中格式错误

Har*_*ran 2 iphone objective-c ios

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

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

NSLog(@"Value %@",strval);
Run Code Online (Sandbox Code Playgroud)

它给出了正确的结果作为S001,但当我在IBAction中打印时,如,

- (IBAction)stringvalue:(id)sender {
NSLog(@"Value %@",strval);
}
Run Code Online (Sandbox Code Playgroud)

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

请帮我..

Pop*_*eye 7

尝试这样的事情

在.h

  @property (nonatomic, strong) NSString *strval;
Run Code Online (Sandbox Code Playgroud)

在.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)
  }
Run Code Online (Sandbox Code Playgroud)

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