组成如下
首先进入的是一个登录页面,登录页面包含UIImageView、UILabel、UITextField和UIButton组件。页面如下。
首先,应该知道,在整个项目文件中,main.m不用改。AppDelegate监控整个APP运行,是单例,即只被初始化一次。创建一个应用程序,把AppDelegate作为程序代理。在AppDelegate.m的didFinishLaunchingWithOptions{}方法中写代码,代码段如下。
- (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文件的viewDidLoad{ }方法中进行页面布局,在 -(void) viewDidLoad { }中放置初始化代码,但是注意不可以在 -(void) viewDidLoad { }中添加与几何相关的代码,即不可以添加任何关于视图形状的初始化信息。
因为已经添加了导航控制器,所以页面顶端会出现导航栏,然后设置标题以及页面颜色。
然后就是往页面中添加各组件,包含一张图片(UIImageView),两个标签(UILabel),两个文本区域(UITextField),一个按键(UIButton)。
代码段如下。
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"登录";
self.view.backgroundColor = [UIColor whiteColor];
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 setTitl