我正在测试一个简单的UIPickerView应用程序,以便我可以将它用作项目中的类... 这段代码与ARC完美配合,但是当我关闭ARC时,构建成功但是当我点击选择器的显示时崩溃...显示的错误是" EXC_BAD_ACCESS(代码= 2,地址= oxc)& #34;在
行如果有人可以提供帮助,我将非常感激......我花了7个多工作日[60小时+]尝试在stackoverflow网站上提供各种选项,但却无法解决问题...不必要说我想学习......return [pickerData1 objectAtIndex:row];
...我已经了解到这一点,阵列存在一个问题......它正在被释放,而它应该被保留直到< 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
非常感谢提前!!
答案 0 :(得分:1)
如果你想知道关闭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文档中了解assign
和strong
之间的区别。
最好将数组更改为强大的属性。虽然没有ARC,强者只是意味着retain
。
干杯,玩得开心。
答案 1 :(得分:0)
不要打扰关闭ARC。您可以选择在file-by-file basis上启用/禁用ARC:
-fobjc-arc
为ARC中无法使用ARC的项目中的文件启用ARC -fno-objc-arc
反之亦然