简体   繁体   中英

Load UIView with nib with user interaction

There are plenty of questions regarding loading UIViews from Nib files, so that isn't the issue. 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.

Scenario 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. 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

[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:

-[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). Should it be?

the designated initializer is initWithFrame

apart from that :: how is the IBOutlet for view?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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