美文网首页iOS成长之路
iOS 入门(1):创建一个 iOS 项目

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

作者: 小仙女的大伯伯 | 来源:发表于2020-08-10 15:51 被阅读0次

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

教程

目录:

  • 1、安装 Xcode
  • 2、第一个 iOS 项目
    • 2.1、创建和运行一个 iOS 项目
    • 2.2、添加一个交互事件

1、安装 Xcode

安装 Xcode 一般有两种方式:

  • 通过 App Store 安装。在 App Store 搜索并安装即可。
  • 到苹果提供的站点下载 Xcode 安装包,自己在本地安装即可。
    • 资源下载中心(需要 Apple Developer 账号登录,这里的资源非常全面)
    • Xcode 下载(这里可以下载到最新 Beta 版,已发布的版本还是会引导你去 App Store 下载)

2、第一个 iOS 项目

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

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

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

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

1.png

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

2.png
3.png

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

4.png

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

2.2、添加一个交互事件

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

改完名后:

5.png

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

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

6.png 7.png

相关文章

网友评论

    本文标题:iOS 入门(1):创建一个 iOS 项目

    本文链接:https://www.haomeiwen.com/subject/nqsjdktx.html