手动管理创建并启动一个StoryBoard

来源:互联网 发布:种子在线观看软件 编辑:程序博客网 时间:2024/06/10 18:22

http://www.xcoder.cn/index.php/archives/329


手动管理创建并启动一个StoryBoard就是让我们自定义一个StoryBoard,然后将其设置为App启动时默认的启动StoryBoard。

一、先创建一个Empty项目,这样做Xcode就不会为我们的项目创建任何文件了,只有一个App的代理。

创建一个Empty的Xcode项目(iOS)

创建一个Empty的Xcode项目(iOS)

二、创建好之后就可以看到项目的文件,系统就只给我们创建了一个主代理文件AppDelegate,其实这个文件都可以不用创建

iOS空项目的目录结构

iOS空项目的目录结构

三、新建一个名为Main的StoryBoard,其实这个名字怎么样都无所谓。

Main.StoryBoard的布局概览

Main.StoryBoard的布局概览

一个新的StoryBoard创建好后场景内是空的,这里先拖入一个View Controller,这第一个View Controller默认就成为了App的入口场景了,在这个View Controller中添加一个Label来说明文件确实被启动了。

四、先看看程序启动后的结果

未加载StoryBoard的项目启动

未加载StoryBoard的项目启动

App启动后是空白的,并未加载StoryBoard。

五、将自定义的StoryBoard设置为主界面

<iframe id="iframeu1595965_0" src="http://pos.baidu.com/vckm?rdid=1595965&amp;dc=2&amp;di=u1595965&amp;dri=0&amp;dis=0&amp;dai=1&amp;ps=2882x523&amp;dcb=BAIDU_SSP_define&amp;dtm=BAIDU_DUP_SETJSONADSLOT&amp;dvi=0.0&amp;dci=-1&amp;dpt=none&amp;tsr=0&amp;tpr=1459384462166&amp;ti=%E6%89%8B%E5%8A%A8%E7%AE%A1%E7%90%86%E5%88%9B%E5%BB%BA%E5%B9%B6%E5%90%AF%E5%8A%A8%E4%B8%80%E4%B8%AAStoryBoard%20%7C%20XCoder%20Studio&amp;ari=1&amp;dbv=2&amp;drs=1&amp;pcs=1903x979&amp;pss=1903x2974&amp;cfv=14&amp;cpl=28&amp;chi=2&amp;cce=true&amp;cec=UTF-8&amp;tlm=1459355662&amp;ltu=http%3A%2F%2Fwww.xcoder.cn%2Findex.php%2Farchives%2F329&amp;ecd=1&amp;psr=1920x1080&amp;par=1920x1040&amp;pis=-1x-1&amp;ccd=24&amp;cja=true&amp;cmi=37&amp;col=zh-CN&amp;cdo=-1&amp;tcn=1459384462&amp;qn=cc4e7c13dc328b9d&amp;tt=1459384462144.25.212.215" width="336" height="280" align="center,center" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="border-width: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: bottom; max-width: 100%;"></iframe>

点击打开XCAppDelegate.m,看看application:函数代码:

- (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.window makeKeyAndVisible];
    return YES;
}

先创建了一个UIWindow,并将其设置为了当前App的UIWindow,将背景色设置为白色,并显示这个UIWindow。

现在更改这个函数,让StoryBoard显示在程序中,更改后的程序代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
 
    UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    self.window.rootViewController = [storyBoard instantiateInitialViewController];
 
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

显示结果如下:

加载StoryBoard的App

加载StoryBoard的App

这下StoryBoard显示出来了。


0 0
原创粉丝点击