2013-05-31 51 views
2

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

使用NFModalPickerView作爲pickerview的委託時,我總是得到一個壞訪問錯誤。如果我把所有的代碼在視圖控制器中使用視圖控制器作爲選擇器視圖的委託它工作正常。我需要一個單獨的類來重新使用modalpickerview,而不是將所有代碼放在每個視圖控制器中。任何人都可以幫助我呢?

+0

在視圖控制器中,你是如何定義'pickerView'的? – Wain

+0

NFModalPickerView * pickerView;也許我應該用__strong來嘗試。 –

+0

該定義在哪裏?與UIPickerView交互時是否是異常? – Wain

回答

0

如果您使用的是ARC,則UIPickerView的delegate可能會自動發佈。或者使用UIViewController作爲dataSourcedelegate作爲UIPickerView,或者如果您使用單獨的對象來管理UIPickerView,請將其作爲屬性保留,以使其不被釋放。

相關問題