EXC_BAD_ACCESS(代码= 1 ...。使用NSObject作为委托时

时间:2013-05-31 15:29:26

标签: iphone ios objective-c delegates uiactionsheet

我在.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

和.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;
}

当使用NFModalPickerView作为pickerview的委托时,我总是遇到错误的访问错误。如果我使用视图控制器作为选择器视图的委托将所有代码放在视图控制器中,它可以正常工作。我需要一个单独的类来重用modalpickerview,而不是总是将所有代码都放在我的每个视图控制器中。有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

如果您使用的是ARC,UIPickerView的delegate可能会自动发布。使用UIViewController作为dataSource,使用delegate作为UIPickerView,或者如果您使用单独的对象来管理UIPickerView,请将其保留为{{1}}一个财产,以便它不被释放。