我正在尝试从完全 Android 的背景中学习 iOS。很抱歉这个超级菜鸟问题,但我想构建一个 UIPickerView Util 类,它可以在我的应用程序中作为一个单独的类一遍又一遍地重用,我收到EXC_BAD_ACCESS消息,我不知道为什么。所以我有两个问题:
我没有看到任何关于将它作为不同的类分开的东西,这是因为这是处理这个问题的不正确方法吗?
这个基本(主要是生成的)代码会给我 EXC_BAD ACCESS 消息有什么问题?我读过这与内存问题有关。我正在使用 ARC,为什么这是一个问题?
这是我正在尝试构建的课程的开始。
头文件
#import <UIKit/UIKit.h>
@interface PickerTools : UIViewController<UIPickerViewDelegate>
@property (strong, nonatomic)UIPickerView* myPickerView;
-(UIPickerView*)showPicker;
@end
实施文件
#import "PickerTools.h"
@implementation PickerTools
@synthesize myPickerView;
- (UIPickerView*)showPicker {
myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
return myPickerView;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent: (NSInteger)component {
// Handle the selection
}
// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
NSUInteger numRows = 5;
return numRows;
}
// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
// tell the picker the title for a given component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *title;
title = [@"" stringByAppendingFormat:@"%d",row];
return title;
}
// tell the picker the width of each row for a given component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
int sectionWidth = 300;
return sectionWidth;
}
@end
这是我从 UITableViewController 类中的方法调用它的方式:
PickerTools *picker = [[PickerTools alloc]init];
[[self view]addSubview:[picker showPicker]];
再次感谢您的帮助!只是想学习!