简体   繁体   English

如何通过复制现有的UIViewController来创建新的UIViewController?

[英]How can I create a new UIViewController by copying an existing one?

I want to create a new UIViewController that is very similar to an existing one. 我想创建一个与现有的UIViewController非常相似的新UIViewController。 I tried copying the files (old.m, old.h, and old.xib) and renaming them (new.m, etc.) The results, when added to the project, compile just fine, but I have a problem in the implementation of initWithNibName; 我尝试复制文件(old.m,old.h和old.xib)并重命名它们(new.m等)。将结果添加到项目中后,可以很好地进行编译,但是我遇到了问题initWithNibName的实现;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        someCustomWork(); // Not important for the problem, but the reason for this function!
    }
    return self;
}

The call to the super initWithNibName always returns (null), where for the original, a valid object is returned. 对超级initWithNibName的调用始终返回(空),对于原始的调用,将返回有效对象。

So, what am I missing here? 那么,我在这里想念什么? How can I best create a new UIViewController by copying the files of an existing one? 如何通过复制现有的UIViewController的文件来最好地创建一个新的UIViewController?

Thanks! 谢谢!

Heritage. 遗产。 Create a new blank VC and in the .h file: 在.h文件中创建一个新的空白VC:

#import "OldViewController.h"

@interface NewViewController : OldViewController

This will make the newVC have all the same functions and properties as the old one. 这将使newVC具有与旧VC相同的功能和属性。 Then override everything that must be different in the new one. 然后覆盖新的所有必须不同的内容。

Not sure about the xib files, never worked with storyboards. 不确定xib文件,也从未使用过故事板。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在一个UIViewController中,如何判断是到下一个UIViewController还是返回到最后一个UIViewController - In one UIViewController, how can I judge it is to to next UIViewController, or return to last UIViewController 如何在一个UIViewController中添加多个UITableView? - How can I add more than one UITableView in a UIViewController? 如何创建一个转接表单 UIViewController 到 UICollectionViewController? - 以编程方式 - How can I create a segue form UIViewController to UICollectionViewController ? - Programmatically 我应该为每个UIViewController创建一个NSOperationQueue吗? - Should I create one NSOperationQueue per UIViewController? 如何在Swift中以编程方式创建新的UIViewController - How to programmatically create a new UIViewController in Swift 我可以在一个应用程序中集成UIViewController和UICollectionViewController吗? - Can I integrate UIViewController and UICollectionViewController in one app? 我可以从我现有的应用程序创建新的 iOS 应用程序吗? - Can I create new iOS app from my existing app? 如何创建从一个实体到另一个现有实体的核心数据关系? - How can I create a Core Data relationship from one entity to another, existing entity? 如何实现从UICollectionViewController到UIViewController的自定义UIViewController转换? - How can I achieve this custom UIViewController Transition from UICollectionViewController to UIViewController? 如何在故事板上创建一个Segue,从编程注册的UICollectionViewCell到新的UIViewController? - How to create a Segue on the Storyboard, from a programmatically registered UICollectionViewCell to a new UIViewController?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM