2013-11-21 16 views
0

我想將NSSting調用爲void方法,但我得到了EXC_BAD_ACCESS錯誤。這會導致什麼?在另一個無效方法問題中調用NSString

我已經在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); 
} 
+0

你使用ARC或手動內存管理? – CarlJ

+0

使用ARC的同期嗎? –

+0

如果您沒有使用ARC,請爲choiceList字符串進行屬性和合成。 – Smita

回答

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

你可以做到以下幾點:

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

OR:

您需要alloc您的字符串來獲取類中訪問它:

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

只需創建一個@property .h文件等,

@property (strong, nonatomic) NSString * choiceList; 
相關問題