我正在尝试将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);
}
答案 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;