iOS学习之第一天尝试应用。
昨天生成了一个简单的单页面应用。组成如下:
界面如下:
用到的基本控件包括 UIImageView 、 UILabel 、 UITextField 、 UIButton
首先,应该知道,在整个项目文件中,main.m不用改。AppDelegate监控整个APP运行,是单例,即只被初始化一次。创建一个应用程序,把AppDelegate作为程序代理。在AppDelegate.m的这里写代码,代码段如下。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//初始化,全屏
self.loginVC = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.loginVC];
//导航控制器,rootviewcontroller是第一个界面,即nav的第一个界面是loginVC
self.window.rootViewController = nav;
//window的第一个界面是nav
[self.window makeKeyAndVisible];
//设置并显示主窗口
return YES;
}
接下来就是ViewController.m中代码的编写,在 -(void) viewDidLoad { }中放置初始化代码,但是注意不可以在 -(void) viewDidLoad { }中添加与几何相关的代码,即不可以添加任何关于视图形状的初始化信息。
因为已经添加了导航控制器,所以页面顶端会出现导航栏,然后设置标题以及页面颜色。
self.title = @"登录"; //导航栏标题
self.view.backgroundColor = [UIColor whiteColor]; //页面的颜色为白色
然后就是往页面中添加各组件,包含一张图片(UIImageView),两个标签(UILabel),两个文本区域(UITextField),一个按键(UIButton)。
代码如下。
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(160, 120, 100, 100)];
//initWithFrame控制位置和尺寸
imageView.image = [UIImage imageNamed:@"123.png"];
[self.view addSubview:imageView];
//注意不要漏掉这句,把imageView加到视图中
UILabel *userLabel=[[UILabel alloc]initWithFrame:CGRectMake(60, 280, 90, 50)];
userLabel.text=@"用户名:";
[self.view addSubview:userLabel];
UITextField *userField=[[UITextField alloc]init];
userField.frame=CGRectMake(130, 280, 210, 40);
userField.borderStyle = UITextBorderStyleRoundedRect; //文本区域的边界风格
userField.layer.borderColor=[UIColor brownColor].CGColor; //文本框的边界颜色
userField.layer.borderWidth=1;
userField.layer.cornerRadius=5;
[self.view addSubview:userField];
UILabel *passLabel=[[UILabel alloc]initWithFrame:CGRectMake(60, 350, 90, 50)];
passLabel.text=@"密 码:";
[self.view addSubview:passLabel];
UITextField *passwordField=[[UITextField alloc]init];
passwordField.frame=CGRectMake(130, 350, 210, 40);
passwordField.borderStyle = UITextBorderStyleRoundedRect;
passwordField.secureTextEntry=YES; //输入的字符变成*号
passwordField.clearButtonMode=UITextFieldViewModeAlways; //有“X”键可以清空文本
passwordField.layer.borderColor=[UIColor brownColor].CGColor;
passwordField.layer.borderWidth=1;
passwordField.layer.cornerRadius=5;
[self.view addSubview:passwordField];
UIButton *but=[UIButton buttonWithType:UIButtonTypeRoundedRect];
but.frame=CGRectMake(110, 450, 210, 60);
[but setTitle:@"确认" forState:UIControlStateNormal];
[but addTarget:self action:@selector(jumpToNext) forControlEvents:UIControlEventTouchUpInside];
//按键跳转事件,self指代自身。selector()里面写的是跳转方法,即跳转到jumpToNext()方法。监听“点击事件”,所以是UIControlEventTouchUpInside
[but setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
but.backgroundColor=[UIColor colorWithRed:1.0 green:0.5 blue:0.5 alpha:1.0];
//alpha:透明度
[self.view addSubview:but];
然后新申明一个-(void) jumpToNext { }方法,可以在里面定义具体的跳转方法。