子类化UIViewController,viewDidLoad重复调用
我将 UIViewController
子类化为 STViewController
,并注意到从 STViewController
继承的类的 viewDidLoad
方法被重复调用。最终导致应用程序崩溃。 STViewController
此时基本上是一个空白的实现。我进行子类化,如下所示:
#import "STViewController.h"
@interface WelcomeViewController : STViewController {
来自 WelcomeViewController.m 的STViewController.h
#import <UIKit/UIKit.h>
@interface STViewController : UIViewController
{
}
@end
STViewController.m
#import "STViewController.h"
@implementation STViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)loadView
{
// Implement loadView to create a view hierarchy programmatically, without using a nib.
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
viewDidLoad()
- (void)viewDidLoad
{
[super viewDidLoad];
// hide the buttons
[[self signUp] setHidden: YES];
[[self logIn] setHidden: YES];
}
I subclassed UIViewController
as STViewController
and noticed that classes inheriting from STViewController
have their viewDidLoad
method being called repeatedly. Ultimately crashing the app. STViewController
is basically a blank implementation at this point. I am subclassing as shown below:
#import "STViewController.h"
@interface WelcomeViewController : STViewController {
STViewController.h
#import <UIKit/UIKit.h>
@interface STViewController : UIViewController
{
}
@end
STViewController.m
#import "STViewController.h"
@implementation STViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)loadView
{
// Implement loadView to create a view hierarchy programmatically, without using a nib.
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
viewDidLoad() from WelcomeViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// hide the buttons
[[self signUp] setHidden: YES];
[[self logIn] setHidden: YES];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在重写
loadView
,但您的实现是空的,并且您没有分配视图。删除loadView
覆盖。来自 UIViewController 类参考(强调我的):
You are overriding
loadView
, but your implementation is empty, and you're not assigning a view. Remove theloadView
override.From UIViewController Class Reference (emphasis mine):