首页 关于 微信公众号
欢迎关注我的微信公众号

iOS 入门(1):创建一个 iOS 项目

本文主要讲的是如何快速建立和运行一个 iOS 项目,并添加简单的按钮和交互处理逻辑。本文的内容只包含最基本的知识点。

教程

目录:

1、安装 Xcode

安装 Xcode 一般有两种方式:

2、第一个 iOS 项目

2.1、创建和运行一个 iOS 项目

在安装好 Xcode 后,我们就可以开始我们的第一个 iOS 项目了。

创建一个 iOS 项目非常简单:

1)打开 Xcode,然后 File -> New -> Project...,就进入了项目创建的对话框。

image

2)在弹出的对话框中,选择 iOS -> Applicaiton -> Single View Application,然后一路 Next 填写好项目信息即可。

image

image

3)项目创建完成后,选择一下执行目标的模拟器或设备,就可以直接编译执行了,但这时候你运行起来的是一个空白的页面。

image

接下来,我们就开始在这个空白页面上动手做点东西。

2.2、添加一个交互事件

1)首先我们要做一件事:给自己的类名加前缀。这是用 Objective-C 做 iOS 开发时必须遵循的一个习惯。修改方式:在对应类的 .h 文件中选中类名 -> 鼠标右键 -> Refactor -> Rename。下面我们要给这个项目中的所有类加一个前缀 ST,表示 Start 的意思。

image

改完名后:

image

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 弹出提示信息和相关的交互界面。

再运行程序,点击按钮你会看到这些页面:

image

image

Demo

你可以按照下面的步骤获取这一节对应的 Demo 的代码:

$ git clone https://github.com/samirchen/iOSStartDemo.git
$ cd iOSStartDemo/iOSStartDemo
$ git fetch origin s1
$ git checkout s1
$ open iOSStartDemo.xcodeproj

Blog

Opinion

Project