gpt4 book ai didi

ios - xcode - 没有 ARC 的 UIPickerView

转载 作者:行者123 更新时间:2023-11-29 02:40:16 30 4
gpt4 key购买 nike

我正在测试一个简单的 UIPickerView 应用程序,以便我可以在我的项目中将它用作一个类...该代码与 ARC 完美配合,但一旦我关闭 ARC,构建就会成功,但当我单击选择器的显示时崩溃...显示的错误是“EXC_BAD_ACCESS(code=2, address=oxc)”

    return [pickerData1 objectAtIndex:row];

如果有人能提供帮助,我将不胜感激……我已经花了超过 7 个工作日 [60 小时以上] 尝试了 stackoverflow 网站上提供的各种选项,但就是无法解决这个问题……不用说我正在努力学习...

...我了解到数组存在问题...它正在被释放,但它应该保留到“objectAtIndex”调用之前...但我已经尝试了无数种方法来解决这个问题没有一个工作......这是因为我还没有深入了解xcode......尝试边做边学......通常我成功地弄清楚如何在我的项目中应用示例代码并成功完成了我的项目的许多复杂部分(这是一个部署了增强现实技术的 iPad 应用程序),但我拼命地停留在这个相当简单的组件上......

这是头文件:

#import <UIKit/UIKit.h>

@interface PickerText : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate>
{
NSArray *pickerData1;
}
@property (retain, nonatomic) IBOutlet UIPickerView *picker1;
@property (retain, nonatomic) IBOutlet UIButton *buttonForSelection1;
@end

这是实现文件

#import "PickerText.h"

@interface PickerText ()

//set tags to enable future incorporation of multiple pickers
#define kPICKER1COLUMN 1
#define kPICKER1TAG 21
#define kButton1Tag 31

@end

@implementation PickerText

@synthesize picker1;
@synthesize buttonForSelection1;

- (void)viewDidLoad
{
[super viewDidLoad];

// assign data in array
pickerData1 = @[@"[tap to select]", @"Item 1", @"Item 2", @"Item 3", @"Item 4", @"Item 5", @"Item 6"];

// Connect data
picker1.tag = kPICKER1TAG;
self.picker1.dataSource = self;
self.picker1.delegate = self;
picker1.hidden = YES;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}

// set number of columns of data in picker view
- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{
if (pickerView.tag == kPICKER1TAG)
return kPICKER1COLUMN;
else { return 0; }
}

// The number of rows of data
- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{
if (pickerView.tag == kPICKER1TAG)
return [pickerData1 count];
else { return 0; }
}

// The data to return for the row and component (column) that's being passed in (ie set the data in picker view)...

//method using NSString
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (pickerView.tag == kPICKER1TAG)
return [pickerData1 objectAtIndex:row];
else { return 0; }
}

// Capture the picker view selection
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (pickerView.tag == kPICKER1TAG)
{
NSString *selection1 = [pickerData1 objectAtIndex:[picker1 selectedRowInComponent:0]];

//check selection
NSLog(@"%@", selection1);

//change display text on button
[buttonForSelection1 setTitle:selection1 forState:UIControlStateNormal];
[buttonForSelection1 setTitle:selection1 forState:UIControlStateSelected];
}

//to hide picker view after selection
pickerView.hidden=YES;
}


- (IBAction)showPicker:(id)sender
{
if ([sender tag] == kButton1Tag)
{
picker1.hidden = NO;
}
}

@end

提前致谢!!

最佳答案

如果您想知道关闭 ARC 时出现的问题。问题在这里:(1)

@interface PickerText : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate>
{
NSArray *pickerData1;
}

和这里(2) -

pickerData1 = @[@"[tap to select]", @"Item 1", @"Item 2", @"Item 3", @"Item 4", @"Item 5", @"Item 6"];

你看,在 ARC 中,默认关联是 strong 所以当你写第二行时, pickerData1 持有对自动释放数组的强引用,并且一切正常.但是如果没有 ARC,默认关联是 assign 因此,因为在第 2 行中您已经分配了一个自动释放数组(它会在稍后的某个时间点释放),所以您会遇到崩溃。您可以在 Apple 文档中了解 assignstrong 之间的区别。

最好将数组更改为强属性。尽管没有 ARC,strong 将仅仅意味着 retain

干杯,玩得开心。

关于ios - xcode - 没有 ARC 的 UIPickerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25880892/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com