本文介绍了UIViewController(init和initWithNibName)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望了解 init initWithNibName 之间的实际差异。 SO答案,例如表明它更好通过 init 间接调用 initWithNibName


  1. 是否有任何情况需要我们定义init和
    initWithNibName?

  2. 在单个程序执行期间,任何Nib文件是否可能需要多次加载

  3. 问题1& 2相互关联?


解决方案

通过init间接调用 initWithNibName:。你只是想在某个时候调用 initWithNibName:。你可以在外部或内部。有些人认为在内部做更好。我实际上有一个名为LayoutUtil的类,我保留布局相关的帮助方法,以避免编写繁琐的布局相关代码一遍又一遍。这里是我的代码加载UIViewController:

  +(id)loadController:(Class)classType {
NSString * className = NSStringFromClass(classType);
UIViewController * controller = [[classType alloc] initWithNibName:className bundle:nil];
return controller;
}

然后我可以这样做:

  MyViewController * c = [LayoutUtil loadController:[MyViewController class]]; 

如果需要,您可以向类中添加一个名为ughhhh的方法,根本不重要。关键是,在init方法中调用initWithNibName不是一个更好的做法,你只是想确保在启动UIViewController的某个时候调用它。

   - (id)ughhhh 
{
self = [super initWithNibName:@Myviewbundle:nil];
if(self!= nil)
{
}
return self;
}

一个nib文件肯定需要多次加载。每次在UIViewController上调用initWithNibName时,都必须加载xib。很多人加载的UIViews不属于UIViewController,像这样:

  [[NSBundle mainBundle] loadNibNamed:@nameOfXIBFile owner:self options:nil]; 

每次调用此函数时,都将加载nib。



在某些情况下,可以缓存nib。一个例子是UITableView - 但是表视图实现它自己的缓存。操作系统不会自动进行任何缓存。



init initWithNibName: / code>是相关的,因为 initWithNibName:自动调用对象上的init。


Wish to know more about the practical differences between init and initWithNibName. SO answers such as this suggests that it is better to call "initWithNibName" indirectly through "init".

  1. Are there any circumstances that we need to define "init" and"initWithNibName" differently ?
  2. Is it possible that any Nib file needs to be loaded more than onceduring a single program execution ?
  3. Are questions 1 & 2 inter-related ?
解决方案

It is not better to call initWithNibName: indirectly through init. You just want to call initWithNibName: at some point. You can do that externally or internally. Some people think it's better to do it internally. I actually have a class called "LayoutUtil" that I keep layout-related helper methods to avoid writing tedious piece of layout-related code over and over. Here is my code to load a UIViewController:

+ (id)loadController:(Class)classType {
    NSString *className = NSStringFromClass(classType);
    UIViewController *controller = [[classType alloc] initWithNibName:className bundle:nil];
    return controller;
}

And then I can just do:

MyViewController *c = [LayoutUtil loadController:[MyViewController class]];

If you want, you could add a method called ughhhh to a class and call it in there, it doesn't matter at all. The point is that it is not a better practice to call initWithNibName in the init method though, you just want to make sure you call it at some point when initiating a UIViewController.

- (id)ughhhh
{
   self = [super initWithNibName:@"Myview" bundle:nil];
   if (self != nil)
   {
   }
   return self;
}

A nib file can definitely need to be loaded more than once. Everytime you call initWithNibName on a UIViewController the xib has to be loaded. A lot of people load UIViews that are not owned by a UIViewController like this:

[[NSBundle mainBundle] loadNibNamed:@"nameOfXIBFile" owner:self options:nil];

Everytime you call this function you will be loading the nib.

There are certain cases where a nib can be cached. An example would be a UITableView -- but the table view implements it's own cache. The operating system isn't doing any caching automatically.

init and initWithNibName: are related in that initWithNibName: automatically calls init on an object.

这篇关于UIViewController(init和initWithNibName)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 17:15