本文主要講的是如何快速建立和運行一個 iOS 項目,并添加簡單的按鈕和交互處理邏輯。本文的內容只包含最基本的知識點。
教程
目錄:
- 1、安裝 Xcode
- 2、第一個 iOS 項目
- 2.1、創建和運行一個 iOS 項目
- 2.2、添加一個交互事件
1、安裝 Xcode
安裝 Xcode 一般有兩種方式:
- 通過 App Store 安裝。在 App Store 搜索并安裝即可。
- 到蘋果提供的站點下載 Xcode 安裝包,自己在本地安裝即可。
2、第一個 iOS 項目
2.1、創建和運行一個 iOS 項目
在安裝好 Xcode 后,我們就可以開始我們的第一個 iOS 項目了。
創建一個 iOS 項目非常簡單:
1)打開 Xcode,然后 File -> New -> Project...
,就進入了項目創建的對話框。
2)在彈出的對話框中,選擇 iOS -> Applicaiton -> Single View Application
,然后一路 Next 填寫好項目信息即可。
3)項目創建完成后,選擇一下執行目標的模擬器或設備,就可以直接編譯執行了,但這時候你運行起來的是一個空白的頁面。
接下來,我們就開始在這個空白頁面上動手做點東西。
2.2、添加一個交互事件
1)首先我們要做一件事:給自己的類名加前綴。這是用 Objective-C 做 iOS 開發時必須遵循的一個習慣。修改方式:在對應類的 .h
文件中選中類名 -> 鼠標右鍵 -> Refactor -> Rename。下面我們要給這個項目中的所有類加一個前綴 ST
,表示 Start 的意思。
改完名后:
2)在應用程序的入口添加加載頁面的代碼。
修改 STAppDelegate.m
代碼如下:
#import "STAppDelegate.h"
#import "STMainViewController.h"
@interface STAppDelegate ()
@end
@implementation STAppDelegate
#pragma mark - UIApplicationDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Init root view controller of the application.
STMainViewController *vc = [[STMainViewController alloc] init];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
return YES;
}
@end
代碼解釋:在 app 啟動的入口,創建一個 STMainViewController 實例,將它設置為 self.window 的 rootViewController。
3)在頁面上添加按鈕、對應的交互事件和響應事件。
修改 STMainViewController.m
代碼如下:
#import "STMainViewController.h"
@interface STMainViewController ()
@end
@implementation STMainViewController
#pragma mark - Lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Setup.
[self setupUI];
}
#pragma mark - Setup
- (void)setupUI {
// Hello button.
UIButton *helloButton = [UIButton buttonWithType:UIButtonTypeSystem];
[helloButton setTitle:@"Hello" forState:UIControlStateNormal];
[helloButton addTarget:self action:@selector(onHelloButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:helloButton];
//// Layout with frame.
//[helloButton setFrame:CGRectMake(0, 0, 60, 40)];
//helloButton.center = self.view.center;
//// Layout with constraint.
helloButton.translatesAutoresizingMaskIntoConstraints = NO; // If you want to use Auto Layout to dynamically calculate the size and position of your view, you must set this property to NO.
[self.view addConstraints:@[
[NSLayoutConstraint constraintWithItem:helloButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:60.0],
[NSLayoutConstraint constraintWithItem:helloButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:40.0],
[NSLayoutConstraint constraintWithItem:helloButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0],
[NSLayoutConstraint constraintWithItem:helloButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]
]];
}
#pragma mark - Action
- (void)onHelloButtonClicked:(id)sender {
NSLog(@"Hello, world!");
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Hello" message:@"Hello, world!" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"Cancle Action");
}];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"OK Action");
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
}
@end
代碼解釋:在 STMainViewController 頁面加載完成的入口,給頁面添加一個按鈕和對應的事件響應方法。在按鈕點擊事件的響應方法中,使用 UIAlertController 彈出提示信息和相關的交互界面。
再運行程序,點擊按鈕你會看到這些頁面: