Objective c 使用封装类中的块和圆弧将子视图添加到ViewController

Objective c 使用封装类中的块和圆弧将子视图添加到ViewController,objective-c,uiview,automatic-ref-counting,objective-c-blocks,addsubview,Objective C,Uiview,Automatic Ref Counting,Objective C Blocks,Addsubview,在我的示例中,我正在寻找一种方法,将UIPickerView添加到像MBProgressHUD这样的ViewController中。我的子视图是一个UIView,它有一个UIPickerView和一个UIButton来选择一个项目 我在不同的ViewController中使用此视图,因此最好将其封装在自己的类中。我的问题是,它只在没有弧的情况下工作。使用ARC时,应用程序崩溃,EXC_BAD_访问代码=1。。。。所以我想我必须把变量设为强。谁能帮帮我 .h文件如下所示: # import &l

在我的示例中,我正在寻找一种方法,将UIPickerView添加到像MBProgressHUD这样的ViewController中。我的子视图是一个UIView,它有一个UIPickerView和一个UIButton来选择一个项目

我在不同的ViewController中使用此视图,因此最好将其封装在自己的类中。我的问题是,它只在没有弧的情况下工作。使用ARC时,应用程序崩溃,EXC_BAD_访问代码=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
  __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 <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
  __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);
    }];

谢谢你的帮助

首先,您似乎应该阅读_strong和_block应该是用户的位置。 改变

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


in-voidaddPickerToView:UIView*查看并选择:NSMutableArray*选项

此答案有用吗?很抱歉,这并不能解决问题。。。CompletionTest就像一个函数指针。。。。与以前一样,复制应用程序崩溃。。。我认为有些变量太弱了。。。没有ARC,代码运行良好。。。为了改变属性,我以前也试过,这也不起作用。这个答案对代码没有影响。将_块添加到非局部变量没有效果,因此无法修复任何问题。在ARC中,分配给块类型的强变量会复制它,因此添加另一个副本不会有什么不同。首先,块只能用于局部变量。不是实例变量,不是属性,不是参数或返回类型。
completitionTest = compblock;
completitionTest = [compblock copy];