国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

IOS傳值--代理傳值,block傳值,NSNotificationCenter傳值

2019-11-14 19:14:05
字體:
來源:轉載
供稿:網友

一:利用代理傳值,就是利用代理進行通信。

接口文件:

#import <Foundation/Foundation.h>

 

@PRotocol Cdelegate <NSObject>

 

-(void)change:(NSString *)name;

 

 

 

@end

 

.h文件

 

@interface ViewController : UIViewController<Cdelegate>

 

.m文件

- (IBAction)pushBB:(id)sender {

    BViewController *bc=[[BViewController alloc]initWithNibName:@"BViewController" bundle:[NSBundle mainBundle]];

    bc.delegate=self;

    [self presentViewController:bc animated:YES completion:nil];

 

}

 

BViewController文件

.h文件

#import "ViewController.h"

#import "Cdelegate.h"

 

 

@interface BViewController : ViewController

 

@property (weak, nonatomic) IBOutlet UITextField *name;

@property(nonatomic,assign)id<Cdelegate> delegate;

@property(nonatomic,copy)ablock block;

- (IBAction)popBB:(id)sender;

@end

 

.m文件

- (IBAction)popBB:(id)sender {

    [self.delegate change:self.name.text]; 

    [self dismissViewControllerAnimated:YES completion:nil];

}

二:block傳值

typedef void (^ablock)(NSString *str);

@property(nonatomic,copy)ablock block;

 

- (IBAction)popBB:(id)sender {

    //[self.delegate change:self.name.text];

    self.block(self.name.text);

    [self dismissViewControllerAnimated:YES completion:nil];

}

 

 

 

- (IBAction)pushBB:(id)sender {

    BViewController *bc=[[BViewController alloc]initWithNibName:@"BViewController" bundle:[NSBundle mainBundle]];

    bc.block=^(NSString *str){

        self.aname.text=str;

    };

    [self presentViewController:bc animated:YES completion:nil];

 

}

 

三:通知

 

   NSDictionary *dic=[NSDictionary dictionaryWithObject:self.name.text forKey:@"name"];

    

    [[NSNotificationCenter defaultCenter]postNotificationName:@"changeText" object:self userInfo:dic];

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

 

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

 

}

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 高碑店市| 都安| 巩留县| 辉南县| 铜鼓县| 东乌| 利川市| 高清| 翁牛特旗| 平南县| 宕昌县| 天水市| 普宁市| 凤翔县| 宁陕县| 康马县| 玛纳斯县| 南溪县| 托克逊县| 北碚区| 西丰县| 昭觉县| 金湖县| 开江县| 和田县| 曲松县| 达孜县| 宾阳县| 岗巴县| 清丰县| 雷州市| 吉林省| 伊金霍洛旗| 海阳市| 广饶县| 仁怀市| 富平县| 泸西县| 临泉县| 电白县| 浦城县|