如何在Xcode 5中使用xib文件代替故事板?

6

我对ios开发非常陌生,我看的很多教程都是遵循Xib的方法,因为它们是在XCode 5之前录制的。如何在Single View Application中使用Xib的方法呢?当我删除Storyboard并选择一个Xib作为主界面时,它不起作用。感谢任何提示。


嗨,开始在iOS平台上编程是很好的。祝你未来挑战一切顺利。你可以在这个链接中找到教程:http://www.raywenderlich.com/tutorials - user2223516
2
@HappyCoding,Ray的网站上没有关于在Xcode 5中使用xib的材料。如果有,请您发布链接。 - Adrian P
请查看此链接:https://dev59.com/5GIk5IYBdhLWcg3wDaQW - user2223516
3个回答

11

在那里添加新文件,在选择CocoCatouch后选择Objective-C类,然后进入下一步。您将看到如下屏幕。


在底部必须选择使用XIB作为用户界面,然后点击下一步。


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:

@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;
}

输入图像说明


在Xcode 9中,我们如何在“Main Interface”项目选项中进行设置,而不需要编写代码。 - Satyam

7

在创建新项目时选择空应用程序模板。
在左侧窗格中选择Cocoa Touch -> Objective-C类 -> 然后输入viewController的名称并选中Xib用户界面选项。

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

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 文件:

- (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

1
你可以通过以下步骤来完成它:-
  • 创建一个新项目
  • 选择单视图应用程序
  • 设置项目名称和其他设置
  • 将项目保存在位置
  • 在左侧的导航器面板中选择项目
  • 从项目中删除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];
    

提供:- MKR

https://dev59.com/5GIk5IYBdhLWcg3wDaQW#19633059

只需检查是否漏掉任何步骤。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接