《iOS 9 开发指南》——第6章,第6.9节实战演练——纯代码实现UI设计

    xiaoxiao2024-04-20  7

    本节书摘来自异步社区《iOS 9 开发指南》一书中的第6章,第6.9节实战演练——纯代码实现UI设计,作者 管蕾,更多章节内容可以访问云栖社区“异步社区”公众号查看

    6.9 实战演练——纯代码实现UI设计iOS 9 开发指南在本节的内容中,将通过具体实例讲解另外一种实现UI界面设计的方法:纯代码方式。在本实例中,将不使用Xcode 7的故事板设计工具,而是用编写代码的方式实现界面布局。

    实例6-2 将Xcode界面连接到代码(1)使用Xcode 7创建一个iOS 9程序,在自动生成的工程文件中删除故事板文件。如图6-33所示。

    (2)开始编写代码,文件AppDelegate.h的具体实现代码如下所示:

    #import <UIKit/UIKit.h> @interface AppDelegate :UIResponder<UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end (3)文件AppDelegate.m的具体实现代码如下所示: #import "AppDelegate.h" @interface AppDelegate () @property (nonatomic , strong) UILabel* show; @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {     // 创建UIWindow对象,并将该UIWindow初始化为与屏幕相同大小     self.window = [[UIWindowalloc] initWithFrame:             [UIScreenmainScreen].bounds];     // 设置UIWindow的背景色     self.window.backgroundColor = [UIColorwhiteColor];     // 创建一个UIViewController对象     UIViewController* controller = [[UIViewControlleralloc] init];     // 让该程序的窗口加载并显示viewController视图控制器关联的用户界面     self.window.rootViewController = controller;     // 创建一个UIView对象     UIView* rootView = [[UIViewalloc] initWithFrame:             [UIScreenmainScreen].bounds];     // 设置controller显示rootView控件     controller.view = rootView;     // 创建一个系统风格的按钮     UIButton* button = [UIButtonbuttonWithType:UIButtonTypeSystem];     // 设置按钮的大小     button.frame = CGRectMake(120, 100, 80, 40);     // 为按钮设置文本     [button setTitle:@"确定" forState:UIControlStateNormal];     // 将按钮添加到rootView控件中     [rootViewaddSubview: button];     // 创建一个UILabel对象     self.show = [[UILabelalloc] initWithFrame:           CGRectMake(60 , 40 , 180 , 30)];     // 将UILabel添加到rootView控件中     [rootViewaddSubview: self.show];     // 设置UILabel默认显示的文本     self.show.text = @"初始文本";     self.show.backgroundColor = [UIColorgrayColor];     // 为按钮的触碰事件绑定事件处理方法     [button addTarget:self action:@selector(tappedHandler:)         forControlEvents:UIControlEventTouchUpInside];     // 将该UIWindow对象设为主窗口并显示出来     [self.windowmakeKeyAndVisible];     return YES; } - (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:. } - (void) tappedHandler: (UIButton*) sender {     self.show.text = @"开始学习iOS吧!"; } @end

    这样就用纯代码的方式实现了一个简单地iOS 9界面程序。

    相关资源:敏捷开发V1.0.pptx
    最新回复(0)