我正在尋找一種方法來添加一個子視圖(在我的例子中是一個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);
}];
感謝你的幫助!
首先,'__block'只能用於局部變量。不是實例變量,不是屬性,不是參數或返回類型。 – newacct