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

简介:

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

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

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


a4d74deeeb2bd64a405ce2e8ee2cf90e8a43b1fc

(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界面程序。

相关文章
|
监控 算法 iOS开发
深入探索iOS函数调用栈:符号化与性能调优实战
在iOS开发中,理解函数调用栈对于性能调优和问题排查至关重要。函数调用栈记录了程序执行过程中的函数调用顺序,通过分析调用栈,我们可以识别性能瓶颈和潜在的代码问题。本文将分享iOS函数调用栈的基本概念、符号化过程以及如何利用调用栈进行性能调优。
387 2
|
JSON 搜索推荐 定位技术
打造个性化天气应用:iOS开发实战
【8月更文挑战第31天】在这篇文章中,我们将一起探索如何从零开始构建一个iOS天气应用。通过简单易懂的步骤,你将学习到如何使用Swift编程语言和苹果的开发工具Xcode来实现这个目标。我们会涉及到用户界面设计、网络编程以及数据解析等关键技能,确保你能够顺利地完成这个项目。无论你是初学者还是有一定经验的开发者,这篇文章都会带给你新的启发和收获。
|
测试技术 Swift iOS开发
探索iOS自动化测试:使用Swift编写UI测试
【8月更文挑战第31天】在软件开发的海洋中,自动化测试是保证船只不偏离航线的灯塔。本文将带领读者启航,深入探索iOS应用的自动化UI测试。我们将通过Swift语言,点亮代码的灯塔,照亮测试的道路。文章不仅会展示如何搭建测试环境,还会提供实用的代码示例,让理论知识在实践中生根发芽。无论你是新手还是有经验的开发者,这篇文章都将是你技能提升之旅的宝贵指南。
|
iOS开发
实战编程·使用SwiftUI从0到1完成一款iOS笔记App(五)(3)
实战编程·使用SwiftUI从0到1完成一款iOS笔记App(五)
478 0
|
前端开发 数据处理 iOS开发
实战编程·使用SwiftUI从0到1完成一款iOS笔记App(五)(2)
实战编程·使用SwiftUI从0到1完成一款iOS笔记App(五)
377 0

热门文章

最新文章