私は完全に Android のバックグラウンドから iOS を学ぼうとしています。超初心者の質問で申し訳ありませんが、アプリ全体で別のクラスとして何度も再利用できる UIPickerView Util クラスを構築したいと考えており、EXC_BAD_ACCESSメッセージを受け取りましたが、その理由がわかりません。だから私は2つの質問があります:
これを別のクラスとして分離することについては何も見たことがありません。これは、この問題を処理する不適切な方法であるためですか?
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]];
ご協力いただきありがとうございます。ただ学ぼうとしているだけです!