今天看了教程,教程是用storyboard来搭建整个程序的界面,我则是用纯代码搭建程序的界面,并实现了基本的功能加法。在整个过程中,我是很有思路的,但是一些关键的地方总是记不住,所以决定做笔记以便以后查询和提高记忆。主要步骤如下3步。

1->创建主窗口

2->搭建界面

3->数据获取与处理

ios快速开发教程 pdf下载 ios开发零基础入门教程_#import


一、准备工作。首先是创建工程,然后因为我是纯代码创建,所以要删除掉不必要的东西,当然留着也没什么影响。

1.将文件storyboard和xib直接删除。//因为纯代码,不需要使用到。

2.设置Deployment Target。//这个根据自己要适配的版本来设置

3.Main Interface 设置为空。//因为主窗口我自己创建,所以就不需要main

4.Device Orientation只保留Portrait。//不做屏幕旋转,所以只保留一个

ios快速开发教程 pdf下载 ios开发零基础入门教程_#import_02


二、代码实现阶段-->设置主窗口和根控制器

1.首先创建一个继承UIViewController的新类HNViewController。

2.在AppDelegate.m中#import"HNViewController.h"。

3.在以下方法中添加如下语句。

4.如果缺少[self.window makeKeyAndVisible]这句话,主窗口将不会显示。

5.需要什么类型的根控制器就用什么类创建。

6.因为根控制器由HNViewController创建,所以根控制器的属性可以到HNViewController中去设置。


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
    // Override point for customization after application launch.
 
    //1.创建主窗口
self.window = [[UIWindowalloc]init];
    //2.设置主窗口的frame
self.window.frame = [UIScreenmainScreen].bounds;
    //3.设置根控制器
    self.window.rootViewController = [[HNViewControlleralloc]init];
    //4.显示主窗口
    [self.windowmakeKeyAndVisible];
    returnYES;
}


三、界面搭建阶段

1.分析界面的控件组成。//由图可以看出界面由2个TextField、3个Label和1个Button组成

2.计算各个控件的frame。//以后进入正式开发,会有说明文档说明全部东西布局的参数,不需要自己计算

3.开始创建控件。//全部在HNViewController.m中创建



#import "HNViewController.h"
 
@interface HNViewController ()

 @end
 
@implementation
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     
    //设置第一个输入框
    UITextField *num1 = [[UITextField alloc] init];
    [num1 setFrame:CGRectMake(20, 40, 80, 30)];
    num1.borderStyle = UITextBorderStyleRoundedRect;
    num1.keyboardType = UIKeyboardTypeDecimalPad;
    [num1 becomeFirstResponder];
     
    //设置第二个输入框
    UITextField *num2 = [[UITextField alloc] init];
    [num2 setFrame:CGRectMake(120, 40, 80, 30)];
    num2.borderStyle = UITextBorderStyleRoundedRect;
    num2.keyboardType = UIKeyboardTypeDecimalPad;
     
    //设置加号+
UILabel *plus = [[UILabel alloc] init];
setFrame:CGRectMake(100, 40, 20, 30)];
setText:@"+"];
    [plus setFont:[UIFont systemFontOfSize:20]];
    [plus setTextAlignment:NSTextAlignmentCenter];
     
    //设置等号=
UILabel *equal = [[UILabel alloc] init];
setFrame:CGRectMake(200, 40, 20, 30)];
setText:@"="];
    [equal setFont:[UIFont systemFontOfSize:20]];
    [equal setTextAlignment:NSTextAlignmentCenter];
     
    //设置结果
UILabel *sum = [[UILabel alloc] init];
    [sum setFrame:CGRectMake(220, 40, 60, 30)];
sum setText:@"0"];
    [sum setTextAlignment:NSTextAlignmentRight];
     
    //设置计算按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
bounds = CGRectMake(0, 0, 100, 50);
center = CGPointMake(self.view.frame.size.width * 0.5, 100);
    [button setTitle:@"计算结果" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
     
    //添加计算按钮的点击事件
    [button addTarget:self action:@selector(calculate) forControlEvents:UIControlEventTouchUpInside];
     
    //设置主界面背景色为white
    self.view.backgroundColor = [UIColor whiteColor];
     
    //将各个子控件添加到UIView中
self.view addSubview:num1];
self.view addSubview:num2];
self.view addSubview:plus];
self.view addSubview:equal];
self.view addSubview:sum];
self.view addSubview:button];   
}

- (void)calculate
{
  NSLog(@"点击了计算按钮");
}

 
/* 
*1.完成了布局。TextField.borderStyle可以设置TextField的边框样式,当然也可以用backgroundImage属性设置。
*2.TextField能唤出键盘。TextField.keyboardType可以设置键盘的类型,UIKeyboardTypeDecimalPad时浮点数键盘。
*3.Label中的内容居中使用setAlignment方法,NSTextAlignmentCenter。
*4.button.center = CGPoint(CGFloat X, CGFloat Y);使用该方法可以令控制在某某点居中。
*5.实验中button字体颜色默认为白色,所以要为其设置字体颜色为黑色;另需设置button的高亮状态,以便查看点击。
*6.控件创建后要设置其frame属性,并添加进父控件中。
*/


四、

数据的获取和操作

1.抓取输入到2个TextField中的数据。

2.点击计算按钮,将2个数据相加并把结果显示到Label。


修改后代码如下


#import "HNViewController.h"
 
@interface  HNViewController ()
(nonatomic,strong)UITextField *textNum1;
(nonatomic,strong)UITextField *textNum2;
(nonatomic,strong)UILabel *label;
 
 @end
 
@implementation
 
- (void)viewDidLoad {
    [superviewDidLoad];
    // Do any additional setup after loading the view.
     
    //设置第一个输入框
    UITextField *num1 = [[UITextFieldalloc]init];
 _textNum1
    [_textNum1setFrame:CGRectMake(20,40,80,30)];
    _textNum1.borderStyle =UITextBorderStyleRoundedRect;
    _textNum1.keyboardType =UIKeyboardTypeDecimalPad;
    [_textNum1becomeFirstResponder];
     
    //设置第二个输入框
    UITextField *num2 = [[UITextFieldalloc]init];
 _textNum2
    [_textNum2setFrame:CGRectMake(120,40,80,30)];
    _textNum2.borderStyle =UITextBorderStyleRoundedRect;
    _textNum2.keyboardType =UIKeyboardTypeDecimalPad;
     
    //设置加号+
 UILabel *plus = [[UILabel  alloc] init];
 setFrame:CGRectMake(100,40,20,30)];
 setText:@"+"];
    [plus setFont:[UIFontsystemFontOfSize:20]];
    [plus setTextAlignment:NSTextAlignmentCenter];
     
    //设置等号=
 UILabel *equal = [[UILabel  alloc] init];
 setFrame:CGRectMake(200,40,20,30)];
 setText:@"="];
    [equal setFont:[UIFontsystemFontOfSize:20]];
    [equal setTextAlignment:NSTextAlignmentCenter];
     
    //设置结果
 UILabel *sum = [[UILabel  alloc] init];
 _label
    [_labelsetFrame:CGRectMake(220,40,60,30)];
_labelsetText:@"0"];
    [_labelsetTextAlignment:NSTextAlignmentRight];
     
    //设置计算按钮
    UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];
bounds =CGRectMake(0,0,100,50);
center =CGPointMake(self.view.frame.size.width *0.5, 100);
    [button setTitle:@"计算结果"forState:UIControlStateNormal];
    [button setTitleColor:[UIColorblackColor]forState:UIControlStateNormal];
    [button setTitleColor:[UIColorgrayColor]forState:UIControlStateHighlighted];
     
    //添加计算按钮的点击事件
    [button addTarget:selfaction:@selector(calculate)forControlEvents:UIControlEventTouchUpInside];
     
    //设置主界面背景色为white
    self.view.backgroundColor = [UIColorwhiteColor];
     
    //将各个子控件添加到UIView中
self.viewaddSubview:_textNum1];
self.viewaddSubview:_textNum2];
self.viewaddSubview:plus];
self.viewaddSubview:equal];
self.viewaddSubview:_label];
self.viewaddSubview:button];   
}
 
 
- (void)calculate
{
 NSString *str1 = _textNum1.text;
 NSString *str2 = _textNum2.text;
 float result = str1.floatValue + str2.floatValue;
 NSString *a = [NSString  stringWithFormat:@"%.4f",result];
_labelsetText:a];   
}
 
 
 
 
/*
 
*1.创建成2个textField成员变量来监听数据的输入,成员变量的生命周期要比局部变量更长,而且操作起来更加简单。
 
*2.创建label成员变量来接收结果。
 
*3.因为TextField掉值时text文本,所以要用floatValue将其转换为浮点型。
 
*4.因为label.text接收的事文本,所以要先将结果用stringWithFormat方法拼接成NSString型。
 
*5.UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]创建一般类型的按钮
 
*/