子类化UIViewController,viewDidLoad重复调用

发布于 2024-12-26 16:15:21 字数 1558 浏览 1 评论 0原文

我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回眸一笑 2025-01-02 16:15:21

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

来自 UIViewController 类参考(强调我的):

您不应该直接调用此方法。视图控制器调用
当请求视图属性但当前为 nil 时使用此方法。
如果您手动创建视图,则必须重写此方法并
用它来创建您的观点。如果您使用 Interface Builder 创建
你的视图并初始化视图控制器——也就是说,你初始化
使用 initWithNibName:bundle: 方法的视图,设置 nibName 和
直接 nibBundle 属性,或创建您的视图和视图
Interface Builder 中的控制器 - 那么您不能覆盖它
方法。

此方法的默认实现会查找有效的笔尖
信息并使用该信息加载关联的 nib 文件。
如果没有指定笔尖信息,默认实现会创建
一个普通的 UIView 对象并使其成为主视图。

如果您重写此方法以手动创建视图,
您应该这样做并将层次结构的根视图分配给
查看属性
。 (您创建的视图应该是唯一的实例,并且
不应与任何其他视图控制器对象共享。)
此方法的自定义实现不应调用 super。

You are overriding loadView, but your implementation is empty, and you're not assigning a view. Remove the loadView override.

From UIViewController Class Reference (emphasis mine):

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文