简体   繁体   English

如何在Xcode 6中使用XIB创建iOS项目?

[英]How to create an iOS project with a XIB in Xcode 6?

I'm trying to create a project in Objective-C language without storyboard with Xcode 6 Beta 5. I have tried and created a empty project but it didn't work as Xcode 5. 我正在尝试用Objective-C语言创建一个没有使用Xcode 6 Beta 5的故事板的项目。我尝试并创建了一个空项目,但它不能用作Xcode 5。

I have read this topic How to create project without story board in Xcode 6 - Swift but it didn't help me. 我已经阅读了这个主题如何在Xcode 6中创建没有故事板的项目 - Swift但它对我没有帮助。

What do you mean it didn't work as Xcode 5? 你是什​​么意思它没有像Xcode 5那样工作? You can create empty project without storyboard and then add your own class with XIB like in Xcode 5: File -> New -> File -> Cocoa Touch Class -> Set "Subclass of:" as (for example) UIViewController and check "Also create XIB file". 您可以创建没有storyboard的空项目,然后在Xcode 5中添加您自己的类XIB:File - > New - > File - > Cocoa Touch Class - > Set“Subclass of:”as(例如)UIViewController并检查“创建XIB文件“。

You first make a new "Single View" project. 您首先要创建一个新的“单一视图”项目。 This starts out with a Storyboard, but as Xcode 6 has removed the option for creating an Empty project, we will just work with it from here. 这开始于一个Storyboard,但是当Xcode 6删除了创建Empty项目的选项时,我们将从这里开始使用它。

You then create a new file for this project, go into the "User Interface" category and select "View". 然后,为该项目创建一个新文件,进入“用户界面”类别并选择“查看”。 I would name it the same name as your original ViewController as it will replace the storyboard we are about to delete from the project. 我将它命名为与原始ViewController相同的名称,因为它将替换我们即将从项目中删除的故事板。

Once the XIB is created, you will want to select it and set the "File's Owner" to point to the "ViewController" class that you want this XIB to link with. 创建XIB后,您需要选择它并将“文件所有者”设置为指向您希望此XIB链接的“ViewController”类。 That is done by going into the Identity Inspector of the File's Owner of the Xib, and changing the default of NSObject to the class name of your view controller. 这是通过进入Xib文件所有者的Identity Inspector,并将NSObject的默认值更改为视图控制器的类名来完成的。

Once done with that, you want to go to the Connections Inspector to link the view of the File's Owner to the view of the XIB. 完成后,您需要转到Connections Inspector以将文件所有者的视图链接到XIB的视图。 Just click the little circle across from "view" and drag it over to your view to connect it. 只需点击“视图”对面的小圆圈,然后将其拖到视图中即可进行连接。 You should then have a connection between view and View. 然后,您应该在视图和视图之间建立连接。

Now the important parts. 现在重要的部分。 Go into your project Target, under the "General" tab. 在“常规”选项卡下进入您的项目目标。 There is a subsection called "Deployment Info". 有一个名为“部署信息”的小节。 In that subsection there is a field for "Main Interface". 在该小节中,有一个“主界面”字段。 This field should be showing the name of the storyboard. 该字段应显示故事板的名称。 You need to delete the value shown in this field, so that the Main Interface is left blank. 您需要删除此字段中显示的值,以便将主界面留空。

Then go into the App Delegate and set the root view controller of your window like you have been for previous versions of Xcode. 然后进入App Delegate并设置窗口的根视图控制器,就像您以前版本的Xcode一样。 Once that is done you should have a running app using your XIB, and you can delete your storyboard from the project without any adverse affects. 完成后,您应该使用XIB运行应用程序,并且可以从项目中删除故事板而不会产生任何不利影响。

I don't know why people are down voting this as it is a legitimate question, so here is what you need to do: 我不知道为什么人们会投票,因为这是一个合理的问题,所以这是你需要做的:

Create an empty project, create a new view controller (File/New/File) - with XIB file if you need one, import the new view controller into your AppDelegate, and set this view controller as the root view controller. 创建一个空项目,创建一个新的视图控制器(文件/新建/文件) - 如果需要,可以使用XIB文件,将新视图控制器导入AppDelegate,并将此视图控制器设置为根视图控制器。

AppDelegate.m: AppDelegate.m:

#import "AppDelegate.h"

// import the view controller you want to be displayed first
#import "FirstViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // create an instance of the view controller you want to be displayed first
    FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
    // set it as the root view controller of the application's window
    [self.window setRootViewController:firstViewController];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

Now, of course, if you wanted the create a tab bar or navigation controller, you would do this a bit differently, however this should be a good starting point for you. 当然,现在,如果你想创建一个标签栏或导航控制器,你会有所不同,但这应该是一个很好的起点。

AppDelegate.h

UINavigationController *nav;

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    ViewController *ll=[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
    nav=[[UINavigationController alloc]initWithRootViewController:ll];
    [self.window setRootViewController:nav];
    [nav setNavigationBarHidden:YES];
    return YES;
}

It is very simple: 这很简单:

  • create empty application project 创建空应用程序项目
  • add to this project New File -> Objective-C class (with .xib file). 添加到此项目新文件 - > Objective-C类(带.xib文件)。 My class is named "ViewController" :) Now you must create UINavigationController in AppDelegate.h eg: 我的类名为“ViewController”:)现在你必须在AppDelegate.h中创建UINavigationController,例如:

    @property (strong, nonatomic) UINavigationController *navController; @property(强,非原子)UINavigationController * navController;

than you must set your navcontroller in 比你必须设置你的navcontroller

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    [self.window makeKeyAndVisible];

    ViewController* homeViewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    _navController = [[UINavigationController alloc] initWithRootViewController:homeViewController];

    self.navController.navigationBarHidden = YES;
    self.window.rootViewController = self.navController;

    return YES;
}

Thats all. 就这样。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM