按钮操作的选取器视图崩溃
我有一个选择器视图,当选择选择器视图的行组件时,它会导航到一个视图,那里有一个按钮更改组。单击该按钮时,选择器视图必须出现,但它没有发生!它崩溃了
这是我的代码:
- (void)viewDidLoad
{
groupArr = [[[NSMutableArray alloc]initWithObjects:@"Family",@"Friends",@"Office",@"Acquaintances", nil]autorelease];
agrpPicker.hidden = YES;
}
#pragma mark-
#pragma mark PickerView Datasource
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
int number = 0;
if (agrpPicker.hidden == NO)
{
number = 1;
}
return number;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
int count = 0;
if (agrpPicker.hidden == NO)
{
count = [self.groupArr count];
}
return count;
}
#pragma mark-
#pragma mark PickerView Delegate
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *str = [[[NSString alloc]init]autorelease];
if (agrpPicker.hidden == NO)
{
str = [self.groupArr objectAtIndex:row];
}
return str;
}
这是显示选择器视图的按钮的操作:
-(IBAction)groupButtonSelceted:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.6];
CGAffineTransform transfrom = CGAffineTransformMakeTranslation(0, 200);
agrpPicker.transform = transfrom;
[UIView commitAnimations];
agrpPicker.hidden = [agrpPicker isHidden] ? NO : YES;
[self.view addSubview:agrpPicker];
}
将该选择器视图连接到IB中存在的选择器视图。
这是崩溃位置的屏幕截图
在控制台中,消息显示为快照下面:
任何专家都可以指出我哪里出错了......
请帮助我您的宝贵建议,提前致谢:)
抱歉,我发现出了什么错误,一旦自动释放,我就没有保留数组,而是:
groupArr = [[[NSMutableArray alloc]initWithObjects:@"Family",@"Friends",@"Office" ,@"熟人", nil]autorelease];
它是 self.groupArr = ....;
感谢所有查看此问题以及为解决问题做出贡献的人:)
I have a picker view,when the row component of a picker view is selected,it navigates to a view,there I have a button changeGroup.On clicking the button the picker view must appear,but it is not happening! Its crashing
Here is my code:
- (void)viewDidLoad
{
groupArr = [[[NSMutableArray alloc]initWithObjects:@"Family",@"Friends",@"Office",@"Acquaintances", nil]autorelease];
agrpPicker.hidden = YES;
}
#pragma mark-
#pragma mark PickerView Datasource
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
int number = 0;
if (agrpPicker.hidden == NO)
{
number = 1;
}
return number;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
int count = 0;
if (agrpPicker.hidden == NO)
{
count = [self.groupArr count];
}
return count;
}
#pragma mark-
#pragma mark PickerView Delegate
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *str = [[[NSString alloc]init]autorelease];
if (agrpPicker.hidden == NO)
{
str = [self.groupArr objectAtIndex:row];
}
return str;
}
This is the action for the button for the picker view to appear:
-(IBAction)groupButtonSelceted:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.6];
CGAffineTransform transfrom = CGAffineTransformMakeTranslation(0, 200);
agrpPicker.transform = transfrom;
[UIView commitAnimations];
agrpPicker.hidden = [agrpPicker isHidden] ? NO : YES;
[self.view addSubview:agrpPicker];
}
connected that picker view to the picker view present in IB.
Here is the screen shot of where it got crashed
and in the console,the message appears as in snap shot below:
Can any expert point me out at where I went wrong....
Please help me out with your valuable suggestions,thanks in advance :)
Sorry guys,I found out what was the mistake,I haven't retained the array once it is autoreleased,instead of:
groupArr = [[[NSMutableArray alloc]initWithObjects:@"Family",@"Friends",@"Office",@"Acquaintances", nil]autorelease];
it is self.groupArr = ....;
Thanks for all who viewed and for those who contributed in fixing the issue :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于自动释放,您的 groupArr 数组正在丢失其内容。所以改变这个
groupArr = [[NSMutableArray alloc]initWithObjects:@"Family",@"Friends",@"Office",@"Acquaintances", nil];
[groupArr 保留];
your groupArr array is loosing its contant due to autorelease. so change this
groupArr = [[NSMutableArray alloc]initWithObjects:@"Family",@"Friends",@"Office",@"Acquaintances", nil];
[groupArr retain];
在.h文件中添加UIPickerViewDelegate。
在 .m 文件中,添加 PickerView 委托和数据源方法。
数据源:
委托:
更多详细信息,请参阅 UIPickerView 类 来自 Apple。
Add UIPickerViewDelegate in .h file.
And in .m file, add the PickerView delegate and datasource methods.
Datasource:
Delegate:
For more details, refer to UIPickerView class from Apple.