1.项结构认识
创建一个名为study的项目,具体创建过程很简单。项目结构如下:
xcode项目结构
2.项目结构说明
study.xcodeproj 项目配置信息,类似与Android项目的manifest文件;
Assets.xcassets 存放图像资源的地方;
info.plist 字符串定义,应用权限等等;
main.m 应用程序主函数,里面定义了应用程序的入口;
AppDelegate 应用程序入口,进行必要参数的初始化及,窗口的初始化;
SenceDelegate 分担一部分Appdelegate的职责;
LaunchScreen.storyboard 软件启动动画,appdelegate初始化过程中的ui展示;
Main.storyboard 应用启动后第一个视图管理器,默认配置绑定的是ViewController。如果使用纯代码,需要在SenceDelegate中初始化视图控制器,并赋值给window对象。
3.启动顺序
main -> AppDelegate -> SceneDelegate -> LaunchScreen.storyboard -> Main.storyboard(ViewController)
4.通过代码创建window对象
UIWindowScene * winScene = (UIWindowScene *) scene;
self.window = [[UIWindow alloc] initWithWindowScene:winScene];
MainViewController *mainVC = [[MainViewController alloc] init];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:mainVC];
[self.window setRootViewController:navVC];
[self.window makeKeyAndVisible];
千万别忘记倒入.h文件,这点确实很不方便:
#import "MainViewController.h"
5.显示HelloWorld!
#import "MainViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (void)loadView {
//初始化根视图
self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
//设置根视图的背景颜色
[self.view setBackgroundColor:UIColor.whiteColor];
//初始化一个显示文本的标签视图
UILabel *helloLable = [[UILabel alloc] initWithFrame:(CGRectMake(0, 20, 150, 150))];
//设置文本内容
[helloLable setText:@"Hello World!"];
//设置文本的颜色
[helloLable setTextColor:UIColor.blackColor];
//将文本视图添加到根视图上
[self.view addSubview:helloLable];
}
- (void)viewDidLoad {
[super viewDidLoad];
//方式一、设置标题
self.title = @"First iOS Demo";
//方式二、设置标题
self.navigationItem.title = @"Hello";
}
@end
6.总结
- 语法使用特点:中括号[]可以用来调用方法或者属性,点(.)只能调用属性;
- UIView必须给定具体的大小及位置才能显示出来;
- 如果要使用类,必须县导入类的头文件(.h),否则将无法使用该类;
- AppDelegate主要做一些库文件的初始化工作,SceneDelegate主要用来处理软件启动动画逻辑。