iOS基础控件随笔代码

UIViewController的生命周期:

1、运行APP

2、-  (void)loadView

{

  [super loadView];

}

3、- (void)viewDidLoad

{

  [super viewDidLoad];

}

4、- (void)viewWillAppear:(BOOL)animated

{

  [super viewWillAppear:animated];

}

5、 - (void)viewDidAppear:(BOOL)animated

{

  [super viewDidAppear:animated];

}

6、 - (void)viewWillDisappear:(BOOL)animated

{

  [super viewWillDisappear:animated];

}

7、 - (void)viewDidDisappear:(BOOL)animated

{

  [super viewDidDisappear:animated];

}

8、- (void)didReceiveMemoryWarning

{

  [super didReceiveMemoryWarning];

}

 1 UILabel标签:
 2
 3 #import <UIKit/UIKit.h>
 4
 5 @interface FirstViewController : UIViewController
 6
 7 @property (nonatomic, strong) UILabel *label;
 8
 9 @end
10
11
12 - (void)viewDidLoad
13 {
14     [super viewDidLoad];
15     _label =[[UILabel alloc] initWithFrame:CGRectMake(100, 50, 220, 40)];
16     _label.text = @"欢迎大家学习ios软件开发";
17     _label.textColor = [UIColor redColor];
18     [self.view addSubview:_label];
19
20 }
 1 UIButton和UILable交互
 2
 3 #import "FirstViewController.h"
 4 #import "GlobalDefine.h"
 5 #import "SecondViewController.h"
 6
 7 @interface FirstViewController ()
 8
 9 @end
10
11 @implementation FirstViewController
12
13 - (void)loadView
14 {
15     [super loadView];
16
17 }
18
19 - (void)viewDidLoad
20 {
21     [super viewDidLoad];
22     _label =[[UILabel alloc] initWithFrame:CGRectMake(100, 50, 220, 40)];
23     _label.text = @"欢迎大家学习ios软件开发";
24     _label.tag = 1;
25     _label.textColor = [UIColor redColor];
26     [self.view addSubview:_label];
27
28     UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
29     button.frame = CGRectMake(SCREENWIDTH - 90, SCREENHEIGHT - 60, 80, 40);
30     [button setTitle:@"下一页" forState:UIControlStateNormal];
31     [self.view addSubview:button];
32
33     //跳转
34     [button addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchUpInside];
35 }
36
37 - (void)next:(id)sender
38 {
39     SecondViewController *tempVC = [[SecondViewController alloc] init];
40
41     [self presentViewController:tempVC animated:YES completion:nil];
42 }
43
44 - (void)viewWillAppear:(BOOL)animated
45 {
46     [super viewWillAppear:animated];
47     if (_label.tag != 1)
48     {
49         _label.text = @"HAHAHAHA";
50     }
51     else
52         _label.tag = 0;
53
54 }
55
56 #import "SecondViewController.h"
57 #import "GlobalDefine.h"
58
59 @interface SecondViewController ()
60
61 @end
62
63 @implementation SecondViewController
64
65 - (void)viewDidLoad {
66     [super viewDidLoad];
67
68
69     UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
70     button.frame = CGRectMake(20, 20, 50, 40);
71     [button setTitle:@"返回" forState:UIControlStateNormal];
72
73     [button setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
74
75     [self.view addSubview:button];
76
77     //跳转
78     [button addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchUpInside];
79 }
80
81 - (void)next:(id)sender
82 {
83
84     [self dismissViewControllerAnimated:YES completion:nil];
85 }
  1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  2
  3     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  4     self.window.rootViewController = [[ViewController alloc] init];
  5     self.window.backgroundColor = [UIColor grayColor];
  6     [self.window makeKeyAndVisible];
  7
  8     return YES;
  9 }
 10
 11 #import <UIKit/UIKit.h>
 12
 13 @interface ViewController : UIViewController
 14
 15 @property (nonatomic, strong) UILabel *label;
 16 @property (nonatomic, strong) UIButton *button;
 17
 18 @end
 19
 20 #import "ViewController.h"
 21 #import "GlobalDefine.h"
 22 #import "SecondViewController.h"
 23
 24 @interface ViewController ()
 25
 26 @end
 27
 28 @implementation ViewController
 29
 30 - (void)viewDidLoad
 31 {
 32     [super viewDidLoad];
 33     _label = [[UILabel alloc] initWithFrame:CGRectMake(SCREENWIDTH / 2 - 150, SCREENHEIGHT / 2 - 15, 300, 30)];
 34     _label.text = @"北京,你好!HELLO";
 35     _label.tag = 1;
 36     _label.textColor = [UIColor whiteColor];
 37     _label.font = [UIFont systemFontOfSize:22];
 38     _label.font = [UIFont boldSystemFontOfSize:23];
 39     _label.font = [UIFont italicSystemFontOfSize:25];
 40     _label.textAlignment = NSTextAlignmentCenter;
 41     [self.view addSubview:_label];
 42
 43     _button = [UIButton buttonWithType:UIButtonTypeSystem];
 44     [_button setTitle:@"Next" forState:UIControlStateNormal];
 45     _button.frame = CGRectMake(SCREENWIDTH - 80, SCREENHEIGHT - 50, 60, 30);
 46     [_button setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
 47     _button.titleLabel.font = [UIFont systemFontOfSize:20];//设置按钮字体大小
 48     [self.view addSubview:_button];
 49
 50     //跳转
 51     [_button addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchUpInside];
 52 }
 53
 54 - (void)next:(id)sender
 55 {
 56     SecondViewController *tempVC = [[SecondViewController alloc] init];
 57     [self presentViewController:tempVC animated:YES completion:nil];
 58 }
 59
 60 - (void)viewWillAppear:(BOOL)animated
 61 {
 62     if (_label.tag != 1)
 63     {
 64         _label.text = @"VIP";
 65     }
 66     else
 67     {
 68         _label.tag = 0;
 69     }
 70 }
 71
 72 - (void)didReceiveMemoryWarning {
 73     [super didReceiveMemoryWarning];
 74     // Dispose of any resources that can be recreated.
 75 }
 76
 77 @end
 78
 79 #import "SecondViewController.h"
 80 #import "GlobalDefine.h"
 81
 82 @interface SecondViewController ()
 83
 84 @end
 85
 86 @implementation SecondViewController
 87
 88 - (void)viewDidLoad {
 89     [super viewDidLoad];
 90     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(SCREENWIDTH / 2 - 130, SCREENHEIGHT / 2 - 20, 260, 40)];
 91     label.font = [UIFont systemFontOfSize:21];
 92     label.text = @"IOS is so easy! most of practice";
 93     label.textColor = [UIColor greenColor];
 94     [self.view addSubview:label];
 95
 96     UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
 97     button.frame = CGRectMake(SCREENWIDTH - 100, SCREENHEIGHT - 50, 80, 30);
 98     [button setTitle:@"返回" forState:UIControlStateNormal];
 99     [button setTintColor:[UIColor whiteColor]];
100     button.titleLabel.font = [UIFont systemFontOfSize:16];
101     [self.view addSubview:button];
102
103     //跳转
104     [button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
105
106 }
107
108 - (void)back:(id)sender
109 {
110     [self dismissViewControllerAnimated:YES completion:nil];
111 }
112
113 - (void)didReceiveMemoryWarning {
114     [super didReceiveMemoryWarning];
115     // Dispose of any resources that can be recreated.
116 }
117
118 /*
119 #pragma mark - Navigation
120
121 // In a storyboard-based application, you will often want to do a little preparation before navigation
122 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
123     // Get the new view controller using [segue destinationViewController].
124     // Pass the selected object to the new view controller.
125 }
126 */
127
128 @end
129
130
131 #ifndef GlobalDefine_h
132 #define GlobalDefine_h
133
134 #define SCREENWIDTH [UIScreen mainScreen].bounds.size.width
135 #define SCREENHEIGHT [UIScreen mainScreen].bounds.size.height
136
137 #endif /* GlobalDefine_h */
时间: 2024-11-06 06:38:19

iOS基础控件随笔代码的相关文章

[iOS基础控件 - 5.5] 代理设计模式 (基于”APP列表&quot;练习)

A.概述 在"[iOS基础控件 - 4.4] APP列表 进一步封装,初见MVC模式”上进一步改进,给“下载”按钮加上效果.功能 1.按钮点击后,显示为“已下载”,并且不可以再按 2.在屏幕中间弹出一个消息框,通知消息“xx已经被安装”,慢慢消失 3.消息框样式为圆角半透明 B.不使用代理模式,使用app空间组和主View之间的父子View关系 1.在主View中创建一个消息框 主View控制器:ViewController.m 1 // 创建下载成功消息框 2 CGFloat labelWid

iOS基础控件UINavigationController中的传值

iOS基础控件UINavigationController中的传值,代理传值,正向传值,反向传值 #import <UIKit/UIKit.h> //声明一个协议 @protocol SendValue<NSObject> //定义一个方法 - (void)sendBtnTitle:(NSString *)title; @end @interface FirstViewController : UIViewController // 定义代理 @property (nonatomi

ios基础控件之开关按钮(UISwitch)

UISwitch控件是iOS开发的基础控件,是非常简单的一个控件,因为它的方法比较少.UISwitch继承于UIControl基类,因此可以当成活动控件使用. 注意:开关状态通过它的on属性进行读取,该属性是一个BOOL属性 创建: UISwitch* mySwitch = [[ UISwitch alloc]initWithFrame:CGRectMake(0.150.0f,100.0f,0.0f,0.0f)]; 可能你会疑问为什么它的大小都设置为0?没错,它的大小你设置是无效的,系统会为你分

[iOS基础控件 6.9.1] 聊天界面Demo 代码

框架: 所有代码文件: Model: 1 // 2 // Message.h 3 // QQChatDemo 4 // 5 // Created by hellovoidworld on 14/12/8. 6 // Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8 // message信息模型,存储聊天记录 9 10 #import <Foundation/Foundation.h> 11 12 typedef en

[iOS基础控件 - 6.6.1] 展示团购数据代码[iOS基础控件 - 6.7] 微博展示 使用代码自定义TableCell(动态尺寸)

A.需求 1.类似于微博内容的展示 2.头像 3.名字 4.会员标志 5.内容 6.分割线 7.配图(可选,可有可无) B.思路.步骤 1.Controller:UITableViewController 改变控制器继承自UITableViewController,storyboard中也同时使用新的TableViewController,其TableView作为启动入口. 2.View:代码自定义cell 使用代码组装每个cell,动态改变控件的位置.尺寸 cell含有一个WeiboFrame

[iOS基础控件 - 5.2] 查看大图、缩放图片代码(UIScrollView制作)

原图: 900 x 1305   拖曳滚动: 缩放:       主要代码: 1 // 2 // ViewController.m 3 // ImageZoom 4 // 5 // Created by hellovoidworld on 14/11/28. 6 // Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface

[iOS基础控件 - 5.4] 广告分页代码(UIScrollView制作)

A.概念 例子就是桌面的APP列表,当APP数量超过一个屏幕,自动进行分页 B.实现思路 1.创建一个UIScrollView,这里设置为宽度跟屏幕相同,高度1/4屏幕高度左右 2.使用代码在UIScrollView中添加ImageView,横向放入多张ImageView 3.设置UIScrollView的contentSize为所有图片的宽度总和 4.要保证UIScrollView的宽度等于一张ImageView的宽度,才能正确分页 C.相关属性 设置属性pageEnable = YES,UI

iOS基础控件之 用代码创建控件,不用storyboard

在开发过程中,并不是每次都通过storyboard拖控件完成UI界面,因为storyboard上面的界面是“固定死”的,有时候可能会在程序运行过程中动态地添加一些新的控件到界面上. 比如QQ的聊天信息,是有人发出一条信息后才动态显示出来的. 因此,需要掌握如何用代码动态地添加控件 实际上,storyboard的本质就是根据图形界面描述转成相应的代码. 实践: // 创建一个自定义的按钮 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCu

[iOS基础控件 - 6.7.1] 微博展示 代码

Controller: 1 // 2 // ViewController.m 3 // Weibo 4 // 5 // Created by hellovoidworld on 14/12/4. 6 // Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "Weibo.h" 11 #import "W