나는 .H 파일에이 코드가 있습니다.EXC_BAD_ACCESS (코드 = 1 ... 대리자로 NSObject의를 사용하는 경우
#import <UIKit/UIKit.h>
@interface NFModalPickerView : NSObject
@end
@protocol NFModalPickerViewDelegate<NSObject>
@optional
- (void)titleSelected:(NFModalPickerView *) modalPickerView title:(NSString *) title;
@required
- (void)done:(NFModalPickerView *) modalPickerView;
@end
@interface NFModalPickerView()
{
id <NFModalPickerViewDelegate> delegate;
}
@property (nonatomic, strong) NSMutableArray * objectArray;
@property (nonatomic, strong) id <NFModalPickerViewDelegate> delegate;
- (void) show;
@end
과하는 .m 파일에이 코드 :
#import "NFModalPickerView.h"
@interface NFModalPickerView()<UIPickerViewDelegate,UIPickerViewDataSource>
@end
@implementation NFModalPickerView
@synthesize objectArray;
@synthesize delegate;
UIActionSheet *actionSheet;
UIPickerView *pickerView ;
UISegmentedControl *closeButton;
- (void) show{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
pickerView = nil;
closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Done"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
closeButton = nil;
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [objectArray count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [objectArray objectAtIndex:row];
}
-(void) dismissActionSheet:(id)sender {
UIActionSheet *actionSheet = (UIActionSheet *)[(UIView *)sender superview];
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
[[self delegate] titleSelected:self title:[objectArray objectAtIndex:row]];
}
@end
및 결국 내의 ViewController이 코드
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
pickerView = [[NFModalPickerView alloc] init];
pickerView.objectArray = [[NSMutableArray alloc] init];
[pickerView.objectArray addObject:@"Don personnel"];
[pickerView.objectArray addObject:@"Don d'entreprise"];
[pickerView setDelegate:self];
[pickerView show];
return NO;
}
pickerview의 대리인으로 NFModalPickerView을 사용할 때 항상 나쁜 액세스 오류를 받고 있어요. 내가 넣으면 모든 뷰 컨트롤러를 뷰 컨트롤러의 델리게이트로 사용하는 뷰 컨트롤러의 코드는 잘 동작합니다. modalpickerview를 다시 사용하기 위해 별도의 클래스가 있어야하며 모든 뷰 컨트롤러에 항상 모든 코드를 넣는 것은 아닙니다. 누구든지 이걸 도와 줄 수 있니?
보기 컨트롤러에서 'pickerView'를 어떻게 정의 했습니까? – Wain
NFModalPickerView * pickerView; 어쩌면 나는 __strong으로 시도해야합니다. –
그 정의는 어디에 있습니까? UIPickerView와 상호 작용할 때의 예외입니까? – Wain