1

MBProgressHUD와 같은 ViewController에 Subview (내 예제에서는 UIPickerView)를 추가하는 방법을 찾고 있습니다. 내 Subview는 항목을 선택하기 위해 UIPickerView와 UIButton이있는 UIView입니다.캡슐화 된 클래스의 블록 및 ARC가있는 ViewController에 Subview 추가

다른 ViewController에서이 뷰를 사용하므로 자체 클래스에서 캡슐화하는 것이 좋습니다. 내 문제는 ARC없이 작동한다는 것입니다. ARC 함께 응용 프로그램 충돌 EXC_BAD_ACCESS CODE = 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 파일 : 대한

__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); 
    }]; 

감사 :

#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 

다음 내가이보기를 추가하려면 당신의 도움!

+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와 같습니다 .... 이전과 같은 방법으로 앱 충돌을 복사하면 ... 일부 변수가 약할 것 같습니다 ... ARC가 없으면 코드 잘 작동 ... 그리고 속성을 변경하려면, 나는 또한 전에 시도,이 또한 작동하지 않습니다. – Gulliva

+0

이 답변은 코드와 아무런 차이가 없습니다. 비 로컬 변수에'__block'을 추가해도 아무 효과가 없으므로 아무 것도 수정할 수 없습니다. ARC에서 블록 유형의 강력한 변수에 할당하면 어쨌든 복사되므로 다른 사본을 추가해도 차이가 발생하지 않습니다. – newacct