简体   繁体   English

通过用户交互用笔尖加载UIView

[英]Load UIView with nib with user interaction

There are plenty of questions regarding loading UIViews from Nib files, so that isn't the issue. 关于从Nib文件加载UIView的问题很多,所以这不是问题。 I have my UIView loading from a Nib file just fine, the trouble is, depending on how I set the view, I either don't have user interaction, or IBActions cause a crash. 我可以从Nib文件加载UIView很好,麻烦在于,取决于我如何设置视图,我是否没有用户交互,或者IBActions导致崩溃。

Scenario 0: 场景0:

- (id) init {
self = [super init];

if (self) {
    [[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil];
    [self addSubview:self.view];
}

return self;

} }

Outcome: This loads the view from the Nib, but UIButtons are not tappable. 结果:这将从Nib中加载视图,但UIButton不可轻敲。 I've also noticed the main view of the View Controller behind this is still interactive, even though this view is supposed to block it. 我还注意到,即使该视图被阻止,该视图后面的视图控制器的主视图仍是交互式的。

Scenario 1: Changing this line 方案1:更改此行

[self addSubview:self.view]

to

self = (MyView*)self.view;

Causes the view to block the main view controller view behind it, and a button subview becomes tappable, but... when I connect the button's selector to an IBAction it causes a crash with the error: 使视图阻塞其后的主视图控制器视图,并且按钮子视图变为可轻敲,但是...当我将按钮的选择器连接到IBAction时,导致崩溃并出现错误:

-[MyView performSelector:withObject:withObject:]: message sent to deallocated instance 0x12f1cff0

Why is my view being deallocated in this scenario? 为什么在这种情况下我的视图会被释放? Before someone asks, I am adding the subview simply like so: 在有人问之前,我就像这样添加子视图:

MyView *myView = [[MyView alloc] init];
[self.view addSubview:myView];

Edit: Interestingly, I also notice that awakeFromNib is never being called (in either scenario). 编辑:有趣的是,我还注意到awakeFromNib从未被调用(在任何一种情况下)。 Should it be? 应该是吗

the designated initializer is initWithFrame 指定的初始化程序是initWithFrame

apart from that :: how is the IBOutlet for view? 除此之外:: IBOutlet如何显示?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM