通知與委托


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        // 委托
        [_delegate buyIphone:@"??"];
    }else if (buttonIndex == 1){
    // 通知
        NSDictionary *dic = @{@"boom":@"??"};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeLabel" object:dic];

    }
}
通知

第一個頁面
1.注冊通知

[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(justDoIt:) name:@"changeLabel" object:nil];

2.拿到暗號,做事情(changeLabel為暗號)

-(void)justDoIt:(NSNotification *)obj{
    NSDictionary *dic = [obj object];
_notificationOrigionLabel.text = dic[@"boom"];
}

第二個頁面
1:對暗號
在button里寫

NSDictionary * dic = @{@"boom":@"??"};
[[NSNotificationCenter defaultCenter] postNotificationName:@"zhadan" object:dic];

changeLabel為暗號

委托
@protocol BuyIphone6sDelegate <NSObject>
-(void)buyIphone:(NSString *)str;
@property(nonatomic,strong)id<BuyIphone6sDelegate>delegate;

【第二個頁面】
1在第二個頁面寫協(xié)議,寫在interface 上面
@protocol BuyIphone6sDelegate <NSObject>
2.在第二個頁面 實例化協(xié)議的變量
-(void)buyIphone:(NSString *)str;
3.讓協(xié)議變量去做做協(xié)議中的方法
@property(nonatomic,strong)id<BuyIphone6sDelegate>delegate;
在button里實現(xiàn)

 UIAlertAction *enter = [UIAlertAction actionWithTitle:@"委托" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [_delegate buyIphone:@"??"];
        }];

方法

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
   // 委托
   [_delegate buyIphone:@"??"];

【第一個頁面】
1.跳轉(zhuǎn)頁面的時候,簽合同。
vc2.delegate = self; self為vc1

2.在interface中實現(xiàn)這個協(xié)議
@interface DPNViewController ()<BuyIphone6sDelegate>

3.在.m中實現(xiàn)協(xié)議方法

-(void)buyIphone:(NSString *)str{

_delegateOriginLabel.text = str;
}

整理

第一個頁面
//NextViewController是push進入的第二個頁面
//NextViewController.h 文件
//定義一個協(xié)議,前一個頁面ViewController要服從該協(xié)議,并且實現(xiàn)協(xié)議中的方法

@protocol NextViewControllerDelegate <NSObject>
- (void)passTextValue:(NSString *)tfText;
@end
@interface NextViewController : UIViewController
@property (nonatomic, assign) id<NextViewControllerDelegate> delegate;
 
@end

//NextViewController.m 文件
//點擊Button返回前一個ViewController頁面

- (IBAction)popBtnClicked:(id)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(passTextValue:)]) {
        //self.inputTF是該頁面中的TextField輸入框
        [self.delegate passTextValue:self.inputTF.text];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

第二個頁面
//ViewController.m 文件

@interface ViewController ()<NextViewControllerDelegate>
@property (strong, nonatomic) IBOutlet UILabel *nextVCInfoLabel; 
@end

//點擊Button進入下一個NextViewController頁面

- (IBAction)btnClicked:(id)sender
{
    NextViewController *nextVC = [[NextViewController alloc] initWithNibName:@"NextViewController" bundle:nil];
    nextVC.delegate = self;//設(shè)置代理
    [self.navigationController pushViewController:nextVC animated:YES];
}

//實現(xiàn)協(xié)議NextViewControllerDelegate中的方法

#pragma mark - NextViewControllerDelegate method
- (void)passTextValue:(NSString *)tfText
{
    //self.nextVCInfoLabel是顯示NextViewController傳遞過來的字符串Label對象
    self.nextVCInfoLabel.text = tfText;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容