IOS传值--代理传值,block传值,NSNotificationCenter传值_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > IOS传值--代理传值,block传值,NSNotificationCenter传值

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

 2015/4/18 2:55:12  varliny  程序员俱乐部  我要评论(0)
  • 摘要:一:利用代理传值,就是利用代理进行通信。接口文件:#import<Foundation/Foundation.h>@protocolCdelegate<NSObject>-(void)change:(NSString*)name;@end.h文件@interfaceViewController:UIViewController<Cdelegate>.m文件-(IBAction)pushBB:(id)sender
  • 标签:iOS not 代理

一:利用代理传值,就是利用代理进行通信。

接口文件:

#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];

 

}

 

上一篇: Android Fragments Advertisements 下一篇: 没有下一篇了!
发表评论
用户名: 匿名