按钮操作的选取器视图崩溃

发布于 2024-12-22 21:54:19 字数 1999 浏览 1 评论 0原文

我有一个选择器视图,当选择选择器视图的行组件时,它会导航到一个视图,那里有一个按钮更改组。单击该按钮时,选择器视图必须出现,但它没有发生!它崩溃了

这是我的代码:

- (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

enter image description here

and in the console,the message appears as in snap shot below:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丶视觉 2024-12-29 21:54:19

由于自动释放,您的 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];

毁我热情 2024-12-29 21:54:19

在.h文件中添加UIPickerViewDelegate。

在 .m 文件中,添加 PickerView 委托和数据源方法。
数据源:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;

委托:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;

更多详细信息,请参阅 UIPickerView 类 来自 Apple。

Add UIPickerViewDelegate in .h file.

And in .m file, add the PickerView delegate and datasource methods.
Datasource:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;

Delegate:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;

For more details, refer to UIPickerView class from Apple.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文