@chenbinghua
2017-04-11T10:39:23.000000Z
字数 2134
阅读 1017
iOS笔记
注册、发送、移除
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 注册通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(action:) name:@"NOTIFICATION_NAME" object:nil];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 发送通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATION_NAME" object:nil userInfo:nil];
}
- (void)action:(NSNotification *)notification {
NSLog(@"通知我啦!");
}
- (void)dealloc{
// 移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
// 通知的响应事件
- (void)action:(NSNotification *)notif{
}
.h文件
#import <UIKit/UIKit.h>
#import "TiHuoObject.h"
@protocol GDQueRenTiHuoTableViewCDelldelegate<NSObject>
@required
- (void)updatedText:(NSString *)text atIndexPath:(NSIndexPath *)indexPath;
@required
- (void)updatedButton:(BOOL)isSelect atIndexPath:(NSIndexPath *)indexPath;
@end
@interface GDQueRenTiHuoTableViewCell : UITableViewCell
@property (nonatomic,weak) id<GDQueRenTiHuoTableViewCDelldelegate> delegate;
- (void)setData:(NSDictionary *)dic;
- (void)setTihuo:(TiHuoObject *)tihuo;
@end
.m文件
if([self.delegate respondsToSelector:@selector(updatedButton:atIndexPath:)]) {
GDQueRenTiHuoViewController *vc = (GDQueRenTiHuoViewController *)self.delegate;
[self.delegate updatedButton:button.selected atIndexPath:[vc.tableView indexPathForCell:self]];
}
if([self.delegate respondsToSelector:@selector(updatedText:atIndexPath:)]) {
GDQueRenTiHuoViewController *vc = (GDQueRenTiHuoViewController *)self.delegate;
[self.delegate updatedText:textField.text atIndexPath:[vc.tableView indexPathForCell:self]];
}
.h文件
#import <UIKit/UIKit.h>
@interface ZSSelectView : UIWindow
typedef void(^ResultBlock)(NSInteger index,NSString *result);
@property (nonatomic,strong) NSArray *contentArray; // 展示数据数组
@property (nonatomic,copy) ResultBlock resultBlock;
- (void)show;
+ (instancetype)selectViewWithTitle:(NSString *)title andContentArray:(NSArray *)contentArray andResultBlock:(ResultBlock) resultBlock;
-(void)tap;
@end
.m文件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (_resultBlock) {
_resultBlock(indexPath.row,_contentArray[indexPath.row]);
}
self.hidden = YES;
[self resignKeyWindow];
}