[xcode6.4,ios8.4]纯代码实现IOS显示的例子
前面的例子,我们用Main.storyboard和代码来实现了简单的例子,这次我们将在Main.storyboard的视图中添加任何控件,而是直接通过代码来实现相同的功能。
先打开Xcode—Create a new Xcode project—Single View Application--输入项目名称,同时选择使用Objective-C语言,设备选择iPhone--接下来系统默认生成一个IOS项目模板。
在开始之前,我们先做些准备工作,将代码中需要使用到的一些方法做成一个工具类,方便调用。
这样,我们在项目下面就可以看到2个新生成的文件:Tool.h,Tool.m
Tool.h
// // Tool.h // demo001 // // Created by zhoujianqiang on 15/8/29. // Copyright (c) 2015年 zhoujianqiang. All rights reserved. // #import <Foundation/Foundation.h> @interface Tool : NSObject @end
Tool.m
// // Tool.m // demo001 // // Created by zhoujianqiang on 15/8/29. // Copyright (c) 2015年 zhoujianqiang. All rights reserved. // #import "Tool.h" @implementation Tool @end
接下来,修改Tool.h内容如下
Tool.h
// // Tool.h // demo001 // // Created by zhoujianqiang on 15/8/28. // Copyright (c) 2015年 zhoujianqiang. All rights reserved. // #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface Tool : NSObject #pragma mark 获取屏幕尺寸 + (CGSize) loadScreenSize; #pragma mark 创建一个文本输入框 + (id) createTextField: (CGRect) rect withDelegate : (id) object withPlaceHold : (NSString*) placehold; #pragma mark 创建一个按钮 + (id) createButton: (CGRect) rect withDelegate : (id) object withAction : (SEL) sel withTitle : (NSString*) title; @end
修改Tool.m内容
Tool.m
// // Tool.m // demo001 // // Created by zhoujianqiang on 15/8/28. // Copyright (c) 2015年 zhoujianqiang. All rights reserved. // #import "Tool.h" @implementation Tool + (CGSize) loadScreenSize{ return [UIScreen mainScreen].bounds.size; } + (id) createTextField: (CGRect) rect withDelegate : (id) object withPlaceHold : (NSString*) placehold{ UITextField* field= [[UITextField alloc] initWithFrame:rect]; field.placeholder=placehold; field.borderStyle=UITextBorderStyleRoundedRect; field.delegate=object; return field; } + (id) createButton: (CGRect) rect withDelegate : (id) object withAction : (SEL) sel withTitle : (NSString*) title{ UIButton* button = [[UIButton alloc] initWithFrame:rect]; //设置标题颜色 [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; //设置标题文字 [button setTitle:title forState:UIControlStateNormal]; //设置按钮操作 [button addTarget:object action:sel forControlEvents:UIControlEventTouchUpInside]; return button; } @end
修改 ViewController.h 内容
ViewController.h
// // ViewController.h // demo001 // // Created by zhoujianqiang on 15/8/28. // Copyright (c) 2015年 zhoujianqiang. All rights reserved. // #import <UIKit/UIKit.h> #import "Tool.h" @interface ViewController : UIViewController <UITextFieldDelegate> #pragma mark 手机号 @property (nonatomic,strong) UITextField *phoneNumber; #pragma mark 密码 @property (nonatomic,strong) UITextField *password; #pragma mark 登录按钮 @property (nonatomic,strong) UIButton *loginButton; #pragma mark 登录按钮对应的点击事件 - (void) login : (UIButton*) btn; @end
修改 ViewController.m 内容
ViewController.m
// // ViewController.m // demo001 // // Created by zhoujianqiang on 15/8/28. // Copyright (c) 2015年 zhoujianqiang. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //初始化视图 [self initViews]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)initViews{ //获取屏幕尺寸 CGSize screenSize = [Tool loadScreenSize]; int screenWidth = screenSize.width; int screenHeight = screenSize.height; NSLog(@"width=%d",screenWidth); NSLog(@"hieght=%d",screenHeight); //定义输入框与手机屏幕边的空隙 int spanToBorder = 25; //定义输入框的宽度和高度 int textFieldWidth = screenWidth-2*spanToBorder; int textFieldHeight = 30; //定义第一个输入框的y坐标 int textFieldFirstY=320; //定义输入框之间高度间隔 int spanToAnotherTextFieldForHeight = 20; //输入框个数索引 int textFieldIndex = 0; //输入框y坐标 int y =0; //添加手机号输入框 textFieldIndex = 0; y =textFieldFirstY+spanToAnotherTextFieldForHeight*textFieldIndex; _phoneNumber=[Tool createTextField:CGRectMake(spanToBorder, y, textFieldWidth, textFieldHeight) withDelegate:self withPlaceHold:@"请输入手机号"]; [self.view addSubview:_phoneNumber]; //添加密码输入框 textFieldIndex++; y =textFieldFirstY+(spanToAnotherTextFieldForHeight+textFieldHeight)*textFieldIndex; _password=[Tool createTextField:CGRectMake(spanToBorder, y, textFieldWidth, textFieldHeight) withDelegate:self withPlaceHold:@"请输入密码"]; [self.view addSubview:_password]; //添加登录按钮 textFieldIndex++; y =textFieldFirstY+(spanToAnotherTextFieldForHeight+textFieldHeight)*textFieldIndex; _loginButton=[Tool createButton:CGRectMake(spanToBorder, y, textFieldWidth, textFieldHeight) withDelegate:self withAction:@selector(login:) withTitle:@"登录"]; [self.view addSubview:_loginButton]; } //UITextFieldDelegate method - (BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return YES; } - (void) login : (UIButton*) btn{ if ([_phoneNumber.text isEqual:@"123"] && [_password.text isEqual:@"456"]) { NSLog(@"登录成功!"); }else{ NSLog(@"登录失败..."); } } @end