ios-day11-02(控制器的多种创建方式。init方法创建、通过storyboard的创建、通过xib创建)

本文深入探讨iOS应用开发过程中的关键技术和Swift编程语言的高效用法,包括UI设计、性能优化、错误处理等核心议题,旨在提升开发者的技术水平与实践能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

源码下载地址:http://download.youkuaiyun.com/detail/liu537192/8491685


核心代码:

//
//  JLAppDelegate.m
//  02-控制器的创建
//
//  Created by XinYou on 15-3-11.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "JLAppDelegate.h"
#import "JLOneViewController.h"
#import "JLTwoViewController.h"
#import "JLThreeViewController.h"

@implementation JLAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    // 设置window的背景色为红色
    self.window.backgroundColor = [UIColor redColor];
    
    // 第1种创建控制器的方式
//    [self method_1];
    
    // 第2种创建控制器的方式
//    [self method_2];
    
    // 第3种创建控制器的方式
    [self method_3];
    
    // 设置window为主窗口,并且显示
    [self.window makeKeyAndVisible];
    
    return YES;
}

/**
 *  创建方式3,通过xib创建
 */
- (void)method_3{

    JLThreeViewController *threeViewController = [[JLThreeViewController alloc] initWithNibName:@"three" bundle:nil];
    
    self.window.rootViewController = threeViewController;
    // 使用这种方式创建控制器,程序会直接奔溃。
    // 因为xib和storyboard不同,storyboard中如果有多个控制器,会加载箭头所指的控制器
    // 而xib没有箭头,所以如果xib中有多个UIView,initWithNibName:方法不知道要加载哪个UIView,这时程序就会奔溃
    
    // 这时我们需要进行一些配置。
    // 1,如图5所示,进入three.xib文件选中右上角的File's Owner,然后设置其Class属性
    // 2,如图6所示,右击File's Owner,设置它的View
    // 这时我们再运行程序,就不会有问题了,效果图如图7所示。
}

/**
 *  创建方式2,通过storyboard创建
 */
- (void)method_2{
    // 1,加载storyboard
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"two" bundle:nil];
    // 2,通过storyboard创建控制器
    
    // 2.1,假设two.storyboard中有多个控制器,使用这种方式来创建控制器,默认创建的是storyboard中箭头所指的控制器
    // 这个箭头是可以拖动的,我们可以拖动它让它指向别的控制器
//    JLTwoViewController *twoViewController = [storyboard instantiateInitialViewController];
    
    // 2.2,如果我们想创建不是箭头所指的控制器,可以给控制器设置Storyboard ID属性,如图2所示
    // 然后使用如下方法来创建
    JLTwoViewController *twoViewController = [storyboard instantiateViewControllerWithIdentifier:@"pink"];
    
    self.window.rootViewController = twoViewController;
    
    // two.storyboard如图3所示
    // 以上代码的效果图如图4所示
}

/**
 *  创建方式1,init方式
 */
- (void)method_1{

    JLOneViewController *oneViewController = [[JLOneViewController alloc] init];
    
    oneViewController.view.backgroundColor = [UIColor blueColor];
    
    self.window.rootViewController = oneViewController;
    // 效果图,如图1所示
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end


图1   图2 

图3 图4



图5


图6


图7



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值