1

我正在尋找一種方法來添加一個子視圖(在我的例子中是一個UIPickerView)到像MBProgressHUD這樣的ViewController。我的子視圖是一個UIView,它有一個UIPickerView和一個UIButton來選擇一個項目。添加子視圖到封裝類的塊和ARC的ViewController

我在不同的ViewControllers中使用這個視圖,所以將它封裝在一個自己的類中會很好。我的問題是,它只適用於沒有ARC。使用ARC應用程序崩潰與EXC_BAD_ACCESS代碼= 1 ....所以我想我必須設置變量強。誰能幫我?!

.h文件看起來如下:

# 

import <Foundation/Foundation.h> 

typedef void(^completition)(NSString *result); 

@interface CDPickerView : NSObject<UIPickerViewDataSource, UIPickerViewDelegate> 

@property(nonatomic, strong) __block UIView *_view; 
@property(nonatomic, strong) __block NSMutableArray *_selection; 

-(void)addPickerToView:(UIView*)view withSelection:(NSMutableArray*)selection andReturnSelectedUsingBlock:(completition) compBlock; 

@end 

.m文件:

#import "CDPickerView.h" 

@interface CDPickerView(){ 
// __strong UIView *_view; 
// __strong NSMutableArray *_selection; 
    __strong void (^completitionTest)(NSString* result); 
} 

@end 

@implementation CDPickerView 


-(id)init{ 

    if(!self){ 
     self = [self init]; 
    } 
    return self; 
} 

-(void)addPickerToView:(UIView *)view withSelection:(NSMutableArray *)selection andReturnSelectedUsingBlock:(completition) compblock{ 
    self._view = view; 
    self._selection = [[NSMutableArray alloc] initWithArray:selection]; 
    completitionTest = compblock; 

    dispatch_async(dispatch_get_main_queue(), ^(void){ 
      [self addSelectionView]; 
    }); 


} 

-(void)addPickerToView:(UIView *)view withSelection:(NSMutableArray *)selection{ 
    __view = view; 
    __selection = [[NSMutableArray alloc] initWithArray:selection]; 
    dispatch_async(dispatch_get_main_queue(), ^(void){ 
     [self addSelectionView]; 
    }); 

} 

-(void)addSelectionView{ 

    __strong UIView *custView = [[UIView alloc]initWithFrame:CGRectMake(5, 5, 310, 300)]; 
    custView.layer.cornerRadius = 10.0; 
    custView.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.90]; 
    __strong UIPickerView *picker = [[UIPickerView alloc]initWithFrame:CGRectMake(5, 5, 290, 200)]; 
    picker.dataSource = self; 
    picker.delegate = self; 
    [custView addSubview:picker]; 
    __strong UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 210, 310, 40)]; 
    [button setTitle:@"Auswählen" forState:UIControlStateNormal]; 
    // button.frame = 
    button.backgroundColor = [UIColor whiteColor]; 
    button.titleLabel.font = [UIFont systemFontOfSize:20.0]; 
    button.titleLabel.textColor = [UIColor blackColor]; 
    [button addTarget:self action:@selector(choiceButtonTapped) forControlEvents:UIControlEventTouchUpInside]; 
    [custView addSubview:button]; 
    [__view addSubview:custView]; 
    [picker reloadAllComponents]; 

} 

-(void)choiceButtonTapped{ 
    UIView *view = [[__view subviews] lastObject]; 
    UIPickerView *picker = [[view subviews] objectAtIndex:0]; 
    NSString *result = [__selection objectAtIndex:[picker selectedRowInComponent:0]]; 
    completitionTest(result); 
    [view removeFromSuperview]; 


} 


-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ 

    return [__selection objectAtIndex:row]; 
} 


-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ 

    return __selection.count; 
} 

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ 
    return 1; 
} 



@end 

要添加這個觀點我做到以下幾點:

__strong NSMutableArray *selection = [[NSMutableArray alloc]initWithObjects:@"Test1",@"Test2",@"Test3",@"Test4", nil]; 

    __strong CDPickerView *picker = [[CDPickerView alloc]init]; 
    [picker addPickerToView:self.view withSelection:selection andReturnSelectedUsingBlock:^(NSString *result) { 
     NSLog(@"Test: %@",result); 
    }]; 

感謝你的幫助!

+0

首先,'__block'只能用於局部變量。不是實例變量,不是屬性,不是參數或返回類型。 – newacct

回答

0

首先看起來您應該閱讀__strong和__block應該是用戶的位置。 變化

@property(nonatomic, strong) __block UIView *_view; 
@property(nonatomic, strong) __block NSMutableArray *_selection; 

@property(nonatomic, strong) UIView *_view; 
@property(nonatomic, strong) NSMutableArray *_selection; 

completitionTest = compblock; 

completitionTest = [compblock copy]; 

在 - (無效)addPickerToView:(UIView的*)視圖withSelection:(NSMutableArray裏* )選擇

+0

此解答是否有用? – Avt

+0

對不起,但這並沒有解決問題... completitionTest就像一個Functionpointer ....與複製應用程序崩潰像以前一樣......我認爲一些變量是脆弱的...沒有弧的代碼工作正常......並改變屬性,我也嘗試過,這也是行不通的。 – Gulliva

+0

這個答案對代碼沒有任何影響。將'__block'添加到非本地變量沒有任何作用,因此無法修復任何內容。在ARC中,分配給塊類型的強變量無論如何都會複製它,因此添加另一個副本不會有什麼區別。 – newacct

相關問題