ios之第一個(gè)圖形化界面
1、創(chuàng)建ios項(xiàng)目
1、create Xcode ->simpleViewApplication -> input Program name
2、在控制器里面加入代碼
我們在viewControl.m里面加上UILabel控件,這個(gè)控件和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];
//創(chuàng)建UILable控件
UILabel* label = [[UILabel alloc] init];
//設(shè)置控件的大小和位置
label.bounds = CGRectMake(0, 0, 100, 100);
//設(shè)置控件的中心點(diǎn)和父view的中心點(diǎn)一致,這樣居中顯示
label.center = self.view.center;
//這里設(shè)置了我的名字
label.text = @"chenyu";
//這里需要添加到父View中
[self.view addSubview:label];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
這個(gè)入口類(viewDidLoad)似于Android中的Activity的onCreate方法,我們一般都是在這里初始化一個(gè)UILabel,以及iOS的坐標(biāo)問題,最后還要記得把這個(gè)控件添加在父控件里面,ios里面的控件可以隨便加,
Android不行,需要繼承View.
我們在main.m文件里面可以看到這個(gè)
int main(int argc, char * argv[]) {
NSLog(@"應(yīng)用程序已完成111");
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
這個(gè)方法我們看到和 C語言
中的main函數(shù)形式是一致的,入口就在這里,這里把整個(gè)應(yīng)用程序的邏輯都托付給了AppDelegate類,在iOS中這種方式叫做代理,而在Android中我們通常叫做回調(diào)機(jī)制,我們知道Android
里面的程序入口不是Activity的onCreate方法,也不是Application的onCreate方法,而是ActivityThread.java里面的main函數(shù)方法,這個(gè)是說的Android應(yīng)用程序,沒有涉及到c/c++那塊。
appDelegate.m這個(gè)類就像Android里面的Activity,都會(huì)有周期,后面再來介紹。
3、運(yùn)行結(jié)果
4、總結(jié)
知道ios程序入口和視圖選擇器,我們可以添加基本的控件來顯示在模擬器上,后面再介紹生命周期。
作者:chen.yu
深信服三年半工作經(jīng)驗(yàn),目前就職游戲廠商,希望能和大家交流和學(xué)習(xí),
微信公眾號(hào):編程入門到禿頭 或掃描下面二維碼
零基礎(chǔ)入門進(jìn)階人工智能(鏈接)