如何在 Xcode 5 中使用 xib 文件而不是故事板?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22241120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:43:30  来源:igfitidea点击:

How can i use xib files instead of storyboard in Xcode 5?

iosxcodestoryboardxib

提问by Umut

I'm very new to ios developing and many of the tutorials i watch follow the xib approach since they were recorded before XCode 5. How can i go with the xib approach in Single View Application? When i delete storyboard and select one of the xib's as the main interface it's not working. Thanks for any tip.

我对 ios 开发非常陌生,我观看的许多教程都遵循 xib 方法,因为它们是在 XCode 5 之前录制的。我如何在单视图应用程序中使用 xib 方法?当我删除故事板并选择 xib 之一作为主界面时,它不起作用。感谢您提供任何提示。

回答by Daxesh Nagar

add new file in that select the cococatouch and select the Objective -C class and then go to the next click.
you show the below screen
at the bottom must select the with xib for user Interface.and next

在其中添加新文件,选择 cococatouch 并选择 Objective -C 类,然后单击下一步。

在底部显示以下屏幕必须为用户界面选择带有 xib 的选项。然后下一步

AppDelegate.h:

AppDelegate.h:

@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    ViewController *viewObj;
    UINavigationController *navObj;
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic)  ViewController *viewObj;
@property (strong, nonatomic) UINavigationController *navObj;

AppDelegate.m:

AppDelegate.m:

@synthesize viewObj,window,navObj;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    viewObj = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    navObj = [[UINavigationController alloc] initWithRootViewController:viewObj];

    window.rootViewController=navObj;
    [window makeKeyAndVisible];

    return YES;
}

enter image description here

enter image description here

回答by bhavya kothari

Select Empty application templatewhile creating new project.
Select Cocoa touch from left pane -> objective-c class -> then give name of viewController and tick option of Xib user interface.

创建新项目时选择空应用程序模板
从左窗格中选择 Cocoa touch -> Objective-c 类 -> 然后给出 viewController 的名称并勾选 Xib 用户界面的选项。

and then in apply below code in appDelegate file:

然后在appDelegate 文件应用以下代码:

appDelegate.h File:

appDelegate.h 文件:

#import <UIKit/UIKit.h>
#import "HomeViewController.h"

@interface HomeAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong,nonatomic)HomeViewController *homeViewController;
@property (strong,nonatomic)UINavigationController *navigationController;

appDelegate.m File:

appDelegate.m 文件:

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

    self.homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
    self.navigationController = [[UINavigationController alloc]initWithRootViewController:self.homeViewController];
    self.window.rootViewController = self.navigationController;


    [self.window makeKeyAndVisible];
    return YES;
}

  @end

回答by Shivaay

you can do it by following these steps:-

您可以按照以下步骤操作:-

  • Create a new project
  • Select Single View Application
  • Set ProjectName and other settings
  • Save Project at location
  • Select Project in Navigator Panel in Left
  • Remove Main.storyboard file from project
  • Add New .xib file in Project
  • Set Main Interface to your .xib file in "General Tab" on right panel.
  • Paste following code in didFinishLaunchingWithOptionsmethod

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[ViewController alloc] initWithNibName:@"YourViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    
  • 创建一个新项目
  • 选择单一视图应用程序
  • 设置 ProjectName 和其他设置
  • 将项目保存在位置
  • 在左侧导航器面板中选择项目
  • 从项目中删除 Main.storyboard 文件
  • 在项目中添加新的 .xib 文件
  • 在右侧面板的“常规选项卡”中将主界面设置为您的 .xib 文件。
  • didFinishLaunchingWithOptions方法中粘贴以下代码

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[ViewController alloc] initWithNibName:@"YourViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    

Courtesy:- MKR

礼貌:- MKR

https://stackoverflow.com/a/19633059/1865424

https://stackoverflow.com/a/19633059/1865424

Just check it have you missed any of the step.

只需检查一下您是否错过了任何步骤。