gpt4 book ai didi

objective-c - 子类化 UIViewController,viewDidLoad 反复调用

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:04:43 24 4
gpt4 key购买 nike

我将 UIViewController 子类化为 STViewController 并注意到从 STViewController 继承的类有它们的 viewDidLoad 方法被重复调用.最终导致应用程序崩溃。 STViewController 在这一点上基本上是一个空白的实现。我正在子类化,如下所示:

#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

来自 WelcomeViewController.m 的 viewDidLoad()

- (void)viewDidLoad
{
[super viewDidLoad];

// hide the buttons
[[self signUp] setHidden: YES];
[[self logIn] setHidden: YES];
}

最佳答案

您正在覆盖 loadView,但您的实现是空的,并且您没有分配 View 。删除 loadView 覆盖。

来自 UIViewController Class Reference (强调我的):

You should never call this method directly. The view controller calls this method when the view property is requested but is currently nil. If you create your views manually, you must override this method and use it to create your views. If you use Interface Builder to create your views and initialize the view controller—that is, you initialize the view using the initWithNibName:bundle: method, set the nibName and nibBundle properties directly, or create both your views and view controller in Interface Builder—then you must not override this method.

The default implementation of this method looks for valid nib information and uses that information to load the associated nib file. If no nib information is specified, the default implementation creates a plain UIView object and makes it the main view.

If you override this method in order to create your views manually, you should do so and assign the root view of your hierarchy to the view property. (The views you create should be unique instances and should not be shared with any other view controller object.) Your custom implementation of this method should not call super.

关于objective-c - 子类化 UIViewController,viewDidLoad 反复调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8866573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com