ios - 在没有 nibs/xibs 的情况下正确使用 loadView 和 viewDidLoad 与 UIViewController

标签 ios cocoa-touch uiviewcontroller

当我在没有 nib 的情况下编程时,我的印象是我需要调用 loadView 来初始化我的 View ,如下所示:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        // Custom initialization
        [self loadView];
    }
    return self;
}

(我设置了nibNameOrNil = nil,因为没有nib。)

然后,我设置 View ,如下所示:

- (void) loadView {
    self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 367)];
    [self viewDidLoad];
}

这是在 viewDidLoad 中做的所有其他事情。

我仍然不确定我是否应该以这种方式调用 loadView 和 viewDidLoad。他们不会被自动调用。

令人困惑的是 UIViewController 类引用的文档:

loadView Discussion

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.

我不明白它是如何工作的——创建一个痛苦的 UIView。

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.

If you want to perform any additional initialization of your views, do so in the viewDidLoad method. In iOS 3.0 and later, you should also override the viewDidUnload method to release any references to the view or its contents.

好了,到此为止没有说viewDidLoad是怎么调用的。所以对于 viewDidLoad:

viewDidLoad Discussion

This method is called after the view controller has loaded its associated views into memory. This method is called regardless of whether the views were stored in a nib file or created programmatically in the loadView method. This method is most commonly used to perform additional initialization steps on views that are loaded from nib files.

被什么调用?

由于这些方法不会在我的代码中自动调用,所以我认为我必须自己调用它们。但我仍然没有从文档中清楚地了解这是正确的做法。

最佳答案

如文档所述,您不应自己调用这些方法。

这是如何工作的,如果 UIKit 的代码确实需要在屏幕上显示该 Controller 的 View 层次结构,那么它会调用您的 loadView 方法。

具体来说,如果任何代码试图读取 View Controller 上的 view 属性,而该属性为 nil,则 UIViewController 的代码将调用 loadView 方法。如果您不自己实现 loadView,则默认实现将退回到尝试从 nib 加载 View 层次结构。

当您尝试呈现您的 View Controller 并且 View 为零时,这一切都会自动发生。 (如果您的 View Controller 必须卸载其 View 以响应内存压力,这可能会在您的应用程序运行时发生多次,这是您“免费”获得的另一种行为,并且您不想称呼自己)

如果这些方法没有在您的 View Controller 中调用,那么您呈现此 View Controller 的方式一定有问题,这会阻止 UIViewController 中的框架代码调用这些方法。发布该代码,有人可以帮助您找出该错误。

不过,在您尝试修复那个错误之前,您应该从您的代码中删除这些错误:

[self loadView]
[self viewDidLoad]

然后在您自己的 viewDidLoad 实现中,您需要使用 [super viewDidLoad] 调用父类(super class)的实现

请记住,在 loadView 中,您唯一的责任是将 self.view 设置为某个有效 View 。而已。然后 UIKit 代码会看到它并为您调用 viewDidLoad

希望对您有所帮助。

关于ios - 在没有 nibs/xibs 的情况下正确使用 loadView 和 viewDidLoad 与 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8876212/

相关文章:

ios - $(APP_BUNDLE_ID) 和 $(APP_NAME) 不起作用

ios - 检查 NSTimer 是否有效时崩溃?

iOS NSUserDefaults standardUserDefaults

ios - 导入 AppDelegate

iPhone UIImagePickerController didFinishPickingImage : UIImage transfer between view controllers?

ios - 如何将整个 Swift 类保存为字符串?

iphone - 更改贝塞尔曲线的颜色

iphone - iOS:监控电池电量使用更多电池?应该自动保存吗?

iOS Swift 弹出 View Controller

ios - 缓慢的 UIViewController 加载时间(缓慢的 ClientState 警告)