// M : Model, 负责获取数据
// V : View, 负责显示
// C : Controller, 负责编写各种逻辑代码, 创建Model和View, 并且把Model设置给View
// Models、 Views、 Controllers、 Others
三者之间的协调关系是:
控制器拿到模型对象赋值给对应的视图,然后视图负责展示,控制器是协调者,用来协调视图和模型的关系,一般情况下,视图可以重用,控制器不可以重用,
九宫格算法及案例:i表示for循环中的 int i; 3表示一行有3个视图;
*
计算当前
UIView
所在的列索引
: columnIndex = i %
3
* 计算当前UIView所在的行索引: rowIndex = i / 3
CGFloat
appViewW =
100
;
CGFloat
appViewH =
120
;
CGFloat leftXmargin = 20 ;
CGFloat middleXmargin = ( self . view . frame . size . width - 2 *leftXmargin - 3 *appViewW) * 0.5 ;
CGFloat topYmargin = 30 ;
CGFloat middleYmargin = topYmargin;
// 2. 添加
for ( int i = 0 ; i < 12 ; i ++) {
// / 1. 添加第一个格子
// 1.1 创建一个视图
// UIView *appView = [[UIView alloc]init];
CZAppView *appView = [ CZAppView loadAppView ]; // 1.2 设置 frame
// 1.3 计算行号和列号
int col = i% 3 ;
int row = i/ 3 ;
CGFloat appViewX = leftXmargin + (appViewW + middleXmargin)*col;
CGFloat appViewY = topYmargin + (appViewH + middleYmargin)*row;
appView. frame = CGRectMake (appViewX, appViewY, appViewW, appViewH);
// 1.4 设置颜色
// appView.backgroundColor = [UIColor redColor];
// 1.5 添加
CGFloat leftXmargin = 20 ;
CGFloat middleXmargin = ( self . view . frame . size . width - 2 *leftXmargin - 3 *appViewW) * 0.5 ;
CGFloat topYmargin = 30 ;
CGFloat middleYmargin = topYmargin;
// 2. 添加
for ( int i = 0 ; i < 12 ; i ++) {
// / 1. 添加第一个格子
// 1.1 创建一个视图
// UIView *appView = [[UIView alloc]init];
CZAppView *appView = [ CZAppView loadAppView ]; // 1.2 设置 frame
// 1.3 计算行号和列号
int col = i% 3 ;
int row = i/ 3 ;
CGFloat appViewX = leftXmargin + (appViewW + middleXmargin)*col;
CGFloat appViewY = topYmargin + (appViewH + middleYmargin)*row;
appView. frame = CGRectMake (appViewX, appViewY, appViewW, appViewH);
// 1.4 设置颜色
// appView.backgroundColor = [UIColor redColor];
// 1.5 添加
[
self
.
view
addSubview
:appView];
ios8创建弹框的方法:UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"输入数字不合理" preferredStyle: UIAlertControllerStyleAlert];
*ios8显示弹框的方法:[self presentViewController:alertVc animated:NO completion:nil];
*ios8添加按钮的方法:[alertVc addAction:[UIAlertAction actionWithTitle:@"取消" style: UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"点击了取消按钮");
NSLog(@"点击了取消按钮");
}]];