delegate,block,notification 传值

本文详细介绍了iOS开发中Delegate、Block及Notification的使用方法与注意事项,包括它们的定义、实现方式及其在不同场景下的应用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

delegate是经典设计模式也就是大部分的语言都可以实现的模式,delegate只是保存了一个对象指针,直接回调,没有额外消耗.assign和weak区别 MRC @property (nonatomic, assign) id delegate; ARC @property (nonatomic, weak) id delegate; block出栈需要将使用的数据从栈拷贝到堆,当然对象的话就是加计数,使用完或者block置nil后才消除。所以我们用block时要进行弱引用: ARC下:weak typeof(self) weakSelf = self; 非ARC下:block typeof(self) weakSelf = self; notification 通知的用法相对就是比较简单的,注意添加和删除通知; iOS属性中常用修饰词的总结

###Delegate

Class A
A.h
@protocol TestViewDelegate <NSObject>
-(void)selectedString:(NSString *)string;
@end
@property(weak, nonatomic) id <TestViewDelegate> testViewDelegate;
A.m
- (IBAction)back:(id)sender {
    if (self.testViewDelegate && [self.testViewDelegate respondsToSelector:@selector(selectedString:)]) {
        [self.testViewDelegate selectedString:@"T - T - Delegate"];
    }
}

复制代码
Class B
#pragma mark - Delegate
- (IBAction)buttonClick:(id)sender {
    DelegateViewController *vc = [[DelegateViewController alloc]init];
    vc.testViewDelegate = self;
    [self presentViewController:vc animated:YES completion:nil];
}
-(void)selectedString:(NSString *)string{
    [self dismissViewControllerAnimated:YES completion:nil];//返回上个页面
    NSLog(@"string-- >%@",string);
}
复制代码

###Block

Class A
A.h
typedef void (^TestViewblock)(NSString *string);
@interface BlockViewController : UIViewController
@property(nonatomic,strong)TestViewblock testViewBlock;
@end
A.m
- (IBAction)back:(id)sender {
    if (_testViewBlock) {
        _testViewBlock(@"T - T - Block");
    }
}

复制代码
Class B
#pragma mark - block
- (IBAction)BlockClick:(id)sender {
    BlockViewController *vc = [[BlockViewController alloc]init];
    [self presentViewController:vc animated:YES completion:nil];
    
    __weak typeof(self) weakSelf=self;//避免block 循环缓存
    vc.testViewBlock=^(NSString *string){
        [weakSelf dismissViewControllerAnimated:YES completion:nil];
        NSLog(@"string-->%@",string);
    };
}
复制代码

###Notification

- (IBAction)back:(id)sender {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"test_notification" object:@"T - T - Notification"];
    [self dismissViewControllerAnimated:YES completion:nil];
}
---------------------------------------
#pragma mark - Notification
- (IBAction)NotificationClick:(id)sender
{
    NotificationViewController *vc = [[NotificationViewController alloc]init];
    [self presentViewController:vc animated:YES completion:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self];//移除通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification:) name:@"test_notification" object:nil];//添加通知
}
-(void)notification:(NSNotification *)notification{
    NSString *sting = [notification object];
    NSLog(@"sting --->%@",sting);
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

复制代码

Github代码链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值