UIViewControllers不检测点击/触摸事件

时间:2014-09-15 22:56:01

标签: ios objective-c uiview uiviewcontroller

我正在以编程方式创建UIViewController。在它的viewDidLoad中,我正在创建自定义UIView组件的实例,该组件具有一堆按钮和文本字段。我将此视图设置为viewControllers视图。

但是,当我选择任何UIButton时,它不会触发任何事件或点击UITextFiled内部也不会调出键盘。

- (void)viewDidLoad
{
    [super viewDidLoad];
    SomeView *sv = [[SomeView alloc] initWithFrame:self.view.bounds];
    self.view.userInteractionEnabled = YES;
    self.view = sv;
    //[self.view addSubView:sv];
    self.title = @"Something";
}

这段代码有什么问题吗?即使添加subivew也行不通。 我添加了tapgesture到sv并且它被检测到了,但是在视图中没有其他任何东西被选中。

我尝试直接在viewcontrollers视图中添加了按钮/ textfiled。然后它工作,但不通过customview组件。

1 个答案:

答案 0 :(得分:0)

尝试对视图进行强引用,可能会被取消分配。

@interface YOURCLASS ()
@property (nonatomic, strong) SomeView *sv;
@end

然后返回尝试将其添加为子视图。看看是否有效。

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.sv = [[SomeView alloc] initWithFrame:self.view.bounds];
    self.view.userInteractionEnabled = YES;
    [self.view addSubView:self.sv];
    self.title = @"Something";
}