16 UIKit简介

说明:Mac应用程序使用的是AppKit框架,而iOS应用程序使用的是UIKit框架,它包含了所有的UI组件和构成iOS应用程序的资源。
注意:iOSOS X存在以下区别

  • 没有shell和控制台
  • 应用程序在Mac电脑的模拟器中运行
  • 无法支持一些无UI界面的API
  • 大部分程序员都认为开发iOS应用更加轻松

项目创建:步骤如下

  1. File->New->New Project(command + shift + n)
  2. 选择应用程序模版:左边列表选择iOS下的Application,然后右边选择Single View Application
  • Master-Detail:用一个导航控制器和一个表视图来显示项目列表遗迹项目的详细信息
  • OpenGL Game:游戏
  • Page-Based:创建电子书式的应用,拥有翻页动画效果(该效果支持ipad)
  • Tabbed:多视图应用程序,底部又一个标签栏并且每个标签都有一个视图香关联的那种应用程序
  • Utility:和Single View Application相似,但还多处一个翻转视图
  • Empty:是一个高级选项,如果没有合适的模版,或是你非常了解如何构建你的应用程序,那么刻意选择使用这个模版
  1. 点击Next按钮,弹出询问程序名等信息的对话框
  • 复选框:不选择Use StoryboardInclude Unit Tests,选中Use Automatic Reference Counting
  • Device Family:选择Universal(意味着可以同时运行在iPhoneiPodiPad上)

Alt text

AppDelegate.h

1
2
3
4
5
6
7
8
9
10
#import <UIKit/UIKit.h>
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>

// 窗口对象
@property (strong, nonatomic) UIWindow *window;
// 视图控制器
@property (strong, nonatomic) ViewController *viewController;

@end

AppDelegate.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;
// 窗口被创建时回被调用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/* 初始化窗口对象 */
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

/* 初始视图控制器 */
// iPhone
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
}
// iPad
else {
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
}

// 将视图控制器的视图添加到应用程序层级
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];

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:.
}

@end

16.1 视图控制器

说明:Cocoa主要使用的是MVC模式

  • 视图:nib文件中获取视图
  • 模型:一组数据
  • 控制器:UIViewController的子类

16.1.1 在Nib文件中添加控件

说明:完成视图的部分

  • 拖进一个TextField对象
  • 拖进一个Label
  • 拖进两个Button

Alt text

16.1.2 视图(Nib文件)和控制器建立连接

说明:打开辅助窗口,通过拖拽完成视图(Nib文件)和控制器(ViewController.h)之间的连接。

  1. 代开辅助窗口:Command+Option+ReturnEditor组中间的按钮
  2. Text FieldLabel创建输出口(outlet):按住control键,将鼠标从视图中的图像元素一直拖到ViewController.h相应位置
    Alt text
    Alt text
  3. 为两个按钮创建操作(Action)
  • Name:操作的名称
  • Type:操作方法参数的类型(默认为id
  • Event:事件类型
  • Arguments:NoneSenderEvent(包含一个UIEvent类型的参数)
    Alt text

16.1.3 完成代码的手动编写部分

说明:包括程序核心功能的实现以及一些事件的回调。

ViewController.h

1
2
3
4
5
6
7
8
9
10
11
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UILabel *resultsField;

- (IBAction)uppercase;
- (IBAction)lowercase;

@end

ViewController.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize textField;
@synthesize resultsField;

/**
* 重写父类的便利构造器:完成视图和控制器的绑定
*/

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (nil != self) {
NSLog(@"init: text %@ / result %@", textField, resultsField);
}
return self;
}

/**
* 系统在nib文件加载和对象初始化完成后调用:从ios 5 不会再调用该方法了
*/

- (void)awakeFromNib {
NSLog(@"awake: text %@ / result %@", textField, resultsField);
}

/**
* 系统在nib文件加载和对象初始化完成后调用
*/

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(@"viewDidLoad: text %@ / results %@", textField, resultsField);

// 设置输入框的placeholder(占位符)
[textField setPlaceholder:@"Enter text here"];
// 设置Label的默认值
resultsField.text = @"Result";
}

/**
* 视图从视图层级中移除后会调用这个方法,可以在这里做一些内存清理的事情
* 该方法已过时
*/

- (void)viewDidUnload {
[self setTextField:nil];
[self setResultsField:nil];
[super viewDidUnload];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

// 视图出现前调用
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
// 视图出现后调用
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
// 视图消失前调用
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
// 视图消失后调用
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}


- (IBAction)uppercase {
NSString *original = textField.text;
NSString *uppercase = [original uppercaseString];
resultsField.text = uppercase;
}

- (IBAction)lowercase {
NSString *original = textField.text;
NSString *lowercase = [original lowercaseString];
resultsField.text = lowercase;
}
@end

16.2 小结