2019-5-16

the Model-View-Controller (MVC) design pattern

Model objects manage the app’s data and business logic.
View objects provide the visual representation of your data.
Controller objects act as a bridge between your model and view objects, moving data between them at appropriate times.

View hierarchies make it easier to manage views.
You can also use views to do any of the following:

1. Respond to touches and other events (either directly or in coordination with gesture recognizers).
2. Draw custom content using Core Graphics or UIKit classes.
3. Support drag and drop interactions.
4. Respond to focus changes.
5. Animate the size, position, and appearance attributes of the view.

Container Views

Content Views

Creating cells lazily reduces the amount of memory the table uses.

A view controller’s main responsibilities include the following:

Updating the contents of the views, usually in response to changes to the underlying data.
Responding to user interactions with views.
Resizing views and managing the layout of the overall interface.
Coordinating with other objects—including other view controllers—in your app.

对象之间的通信 一般有三种方式 代理、block、通知

方法的内存实在栈区 当使用copy修饰的时候,系统会把改block实现拷贝一份到堆区

传递的时候,对于基本数据类型的外部变量来说,传递的仅仅是值,也就是说这个局部变量是不能被修改的,如果想修改值,会使用__block来修饰这个变量。这样一来,系统在传递的时候,传的就是外部变量的地址,这样我们就可以修改值了。

closure 闭包

闭包就是能够读其他函数内部变量的函数
返回值(^块对象名称)(参数列表类型) = ^(参数列表){块对象中的代码}
存放地址的内存别名就是指针 int *p = &a;

1.反向传值

2.在方法中定义block回调传值

3.做为参数传递

iOS开发之友盟的集成
社会化分享SDK U-Share
消息推送SDK U-Push 当前版本3.2.4
移动统计SDK U-App

成功的实现了一个代理

首先要声明一个协议        
               @protocol btnClickedDelegate <NSObject>

定义点击事件方法,
-(void)btnClicked
声明一个delegate的变量
@property (nonatomic,weak) id
在cell中的点击事件中调用协议中的方法
[self.btnClickedDelegate btnClicked]
首先将视图设置为代理本身
view.btnClickedDelegate = self
在viewController中实现代理
-(void)btnClicked

故事的开头是这样的:我在写一个项目的过程中自定义了一个View叫做BSPopupView,这个View是一个浮窗,浮窗上面有三行内容,每行内容中呢有一个图标,一行文字和一个点击按钮。当我在对应的ViewController中使用这个自定义的View的时候。我该在哪里处理View中按钮的点击事件呢?很明显的是应该将其放到ViewController中来处理,可是我该怎么做到呢?

我浪迹于各种博客,网站。略模的寻找到了一些线索。大致的方案呢有如下的三种:首先是可以采用发送通知的方式来处理,其次是使用代理的方式来处理,最后是采用Block的方式来处理。这算是给了上、中、下三策啊。可是问题又来了,谁是上策,谁是中策,谁又是下策呢?那我只能挨个儿试试啦。

既然有三种方式可用,那么这三种方式肯定是有各自独特的适用范围和情况的。就通知来说的话,更适用于两个对象没有关系,或者一个对象要和多个对象通信,那么可以采用通知中心。

使用的步骤如下:

1.创建一个通知
NSNotification *notification = [NSNotification notificationWithName:kNotificationWithClickZFBButton object:nil userInfo:nil];

[[NSNotificationCenter defaultCenter] postNotification:notification];

2.在需要处理的地方监听一下
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kNotificationWithClickZFBButton:) name:kNotificationWithClickZFBButton object:nil];

通知说完说代理 代理的使用步骤如下。

1、第一步可以在BSPopView中声明一个协议,声明的方法如下语句:

btnClickedDelegate
1
2
3
4
5
```

2、第二步,为这个声明的协议添加对应的定义点击事件方法,如下语句:

``` -(void)btnClicked;

3、第三步,在BSPopView的头文件中声明一个delegate的属性,可以采用如下的代码:

1
@property (nonatomic,weak) id<btnClickedDelegate>

4、第四步,在BSPopView的实现文件中添加当按钮被点击的时候调用协议中的方法,语句如下

1
[self.btnClickedDelegate btnClicked]

5、第五步,此时就可以到对应的ViewController中来实现在BSPopView中定义的代理了
6、第六步,首先需要继承这个代理,代码如下:

1
@interface BSStopPayViewController () <UITableViewDelegate, UITableViewDataSource,btnClickedDelegate>

7、第七步,然后将试图的代理设置为controller自己,代码如下

1
2
BSPopupView *view = [[BSPopupView alloc] init];
view.btnClickedDelegate = self

8、第八步,在viewController中实现在BSpopView中定义的代理方法,代码如下

1
2
3
                    -(void)btnClicked{
NSLog(@"您在BSPopView中定义的代理方法被成功的实现了!");
}

然后可以说说什么情况下使用代理,就是当两个对象有包含关系的时候就可以使用代理了。如果一个对象想让另一个对象干的不只一件事情,那么就使用代理。如果一个对象想让另一个对象干只有一件事情,那么可以采用BLOCK的方式来实现。