Search code examples
iosnsstringuipickerview

Calling NSString in another void method issue


I am trying to call NSSting into a void method but I am getting EXC_BAD_ACCESS Error. What would be causing this?

I have made a brakepoint(debug) to NSLog in btnPickerDoneTouchHandler but choiceList is empty.

.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);
}

Solution

  • Try this -

    In your .h file

    @property (strong, nonatomic) NSString  * choiceList;
    

    In your .m file

    @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);
    }