在另一个void方法问题中调用NSString

时间:2013-11-21 13:17:52

标签: ios nsstring uipickerview

我正在尝试将NSSting称为void方法,但我得到 EXC_BAD_ACCESS错误。会导致这种情况的原因是什么?

我在btnPickerDoneTouchHandler中为NSLog做了一个刹车点(调试),但是choiceList是空的。

·H

@interface controlView : UIViewController{
      NSString *choiceList;
}

.M:

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    return [res count];
}



-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

    return [[res objectAtIndex:row]objectForKey:@"choice_name"];   
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
      choiceList = [NSString stringWithFormat:@"<r_PM act='closepos_choice' cl_choice='%d'/>", row];

      NSLog(@"You have selected row %@", choiceList);
}


- (void)btnPickerDoneTouchHandler{
      //I have put here the brakepoint.
      NSLog(@"choiceList is %@", choiceList);
}

3 个答案:

答案 0 :(得分:3)

试试这个 -

在你的.h文件中

@property (strong, nonatomic) NSString  * choiceList;

在你的.m文件中

@synthesize choiceList;

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
  self.choiceList = [NSString stringWithFormat:@"<r_PM act='closepos_choice' cl_choice='%d'/>", row];

  NSLog(@"You have selected row %@", self.choiceList);
}


- (void)btnPickerDoneTouchHandler{
    //I have put here the brakepoint.
    NSLog(@"choiceList is %@", self.choiceList);
}

答案 1 :(得分:1)

您可以执行以下操作:

·H:

@interface controlView : UIViewController 
@property (strong, nonatomic) NSString  * choiceList;

.M:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
      self.choiceList = [NSString stringWithFormat:@"<r_PM act='closepos_choice' cl_choice='%d'/>", row];

      NSLog(@"You have selected row %@", choiceList);
}


- (void)btnPickerDoneTouchHandler{
      //I have put here the brakepoint.
      NSLog(@"choiceList is %@", self.choiceList);
}

您需要alloc您的字符串才能在课程中访问它:

  choiceList = [[NSString alloc]initWithString:@"<r_PM act='closepos_choice' cl_choice='%d'/>", row];

答案 2 :(得分:0)

只需在.h文件中创建 @property ,例如

@property (strong, nonatomic) NSString  * choiceList;