我的故事板中有一个视图,我为其分配了一个名为“MainView”的标识符。但是,如果我将其视图添加到子视图中,则接下来的所有内容都会产生崩溃(例如按下按钮)
MainViewController *mvc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainView"];
[self.view addSubview:mvc.view];
这是按钮触发的操作:(MainViewController.h)
-(IBAction)showUsername:(id)sender{
[testLabel setText:@"username"];
}
和崩溃日志:
-[MainViewController performSelector:withObject:withObject:]: message sent to deallocated instance 0x44e0810
我用的是ARC。
处理这个问题的最佳方法是使用属性。就是这样:
在你的 .h 文件中:
#import "MainViewController.h"
@interface MyClass : UIViewController
@property (strong, nonatomic) MainViewController *mvc;
@end
在您的 .m 文件中:
#import "MyClass.h"
@implementation MyClass
@synthesize mvc;
// Your code here
- (void)yourMethodHere {
self.mvc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainView"];
[self.view addSubview:mvc.view];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)