简体   繁体   English

EXC_BAD_ACCESS(Code = 1…..使用NSObject作为委托时

[英]EXC_BAD_ACCESS(Code=1…. when using NSObject as a delegate

I have this code in the .h file : 我在.h文件中有以下代码:

#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

and this code in the .m file : 以及.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

and finally this code in my ViewController 最后这个代码在我的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;
}

I'm always getting a bad access error when using NFModalPickerView as the delegate of the pickerview. 使用NFModalPickerView作为pickerview的委托时,我总是遇到错误的访问错误。 If i put all the code in the view controller using the view controller as the delegate of the picker view it works fine. 如果我使用视图控制器作为选取器视图的委托,将所有代码都放在视图控制器中,则可以正常工作。 I need to have a separate class to reuse the modalpickerview and not always put all the code in each of my view controllers. 我需要有一个单独的类来重用modalpickerview,而不是总是将所有代码放在每个视图控制器中。 Anyone can help me with this? 有人可以帮我吗?

If you are using ARC , UIPickerView 's delegate might be auto-released. 如果使用的是ARC ,则UIPickerView的delegate可能会自动释放。 Either use the UIViewController as the dataSource and the delegate for the UIPickerView , or if you are using a separate object to manage the UIPickerView , keep it as a property so that it is not released. 可以将UIViewController用作UIPickerViewdataSourcedelegate ,或者如果您使用单独的对象来管理UIPickerView ,请将其保留为属性,以使其不被释放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM