ios之第一个图形化界面

264 阅读2分钟

1、创建ios项目

1、create Xcode ->simpleViewApplication -> input Program name 

2、在控制器里面加入代码

我们在viewControl.m里面加上UILabel控件,这个控件和Android 里面的TextView类似,具体代码如下

//
//  ViewController.m
//  SecondHello
//
//  Created by 1111 on 17/7/31.
//  Copyright © 2017年 sangfor. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建UILable控件
    UILabel* label = [[UILabel alloc] init];
    //设置控件的大小和位置
    label.bounds = CGRectMake(0, 0, 100, 100);
    //设置控件的中心点和父view的中心点一致,这样居中显示
    label.center = self.view.center;
    //这里设置了我的名字
    label.text = @"chenyu";
    //这里需要添加到父View中
    [self.view addSubview:label];
}

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

@end

这个入口类(viewDidLoad)似于Android中的Activity的onCreate方法,我们一般都是在这里初始化一个UILabel,以及iOS的坐标问题,最后还要记得把这个控件添加在父控件里面,ios里面的控件可以随便加, Android不行,需要继承View.

我们在main.m文件里面可以看到这个

int main(int argc, char * argv[]) {
    NSLog(@"应用程序已完成111");
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

这个方法我们看到和 C语言 中的main函数形式是一致的,入口就在这里,这里把整个应用程序的逻辑都托付给了AppDelegate类,在iOS中这种方式叫做代理,而在Android中我们通常叫做回调机制,我们知道Android 里面的程序入口不是Activity的onCreate方法,也不是Application的onCreate方法,而是ActivityThread.java里面的main函数方法,这个是说的Android应用程序,没有涉及到c/c++那块。

appDelegate.m这个类就像Android里面的Activity,都会有周期,后面再来介绍。

3、运行结果

4、总结

知道ios程序入口和视图选择器,我们可以添加基本的控件来显示在模拟器上,后面再介绍生命周期。