iOS学习笔记(7)视图控制器之间传递参数的三种方式

视图控制器之间传递参数的三种方式:1.委托(delegate) 2. block    3.通知(NSNotificationCenter


1.委托


B视图切换到A视图并且携带参数

在B视图中制定协议,A视图来尊从协议

B视图

@protocol ModelDelegate <NSObject>


@optional //可选,我们可以实现,也可以不实现

@required //必须要实现,默认@reqired

-(void)textFieldStr:(NSString*)str;


@end


@interface SecondViewController : UIViewController<UITextFieldDelegate>


@property(nonatomic,assign)id<ModelDelegate> delegate;//代理对象



@end

===========

UITextField *textField=(UITextField*)[self.view viewWithTag:12];

    //判断delegate不为空并且我们协议中的方法实现

    if (_delegate!=nil && [_delegate respondsToSelector:@selector(textFieldStr:)]) {//如果不为空说明当前的代理对象就是第一个视图控制器对象

        NSLog(@"//判断delegate不为空并且我们协议中的方法实现");

        //我们的代理回传数据

        [_delegate textFieldStr:textField.text];

    }

====================================

A视图

 SecondViewController *second=[[SecondViewController alloc] init];

    

    second.delegate=self;//将代理对象指向第一个视图控制器

    //建立代理关系

    [self presentViewController:second animated:YES completion:^{

    }];

    [second release];


//协议中的方法

-(void)textFieldStr:(NSString *)str

{

    UITextField *textField=(UITextField*)[self.view viewWithTag:12];

    textField.text=str;

}

==============================================



2.block

B视图切换到A视图,并且携带参数

那么B视图创建block

#import <UIKit/UIKit.h>

typedef  void(^TextFiledBlock)(NSString *);

@interface SecondViewController : UIViewController


@property (nonatomic,copy)TextFiledBlock textfiledBlock;

@property (nonatomic,copy)void(^blockTest)(NSString *str);

@end


 [self.navigationController popViewControllerAnimated:YES];

    UITextField *textField=(UITextField*)[self.view viewWithTag:10];


    if(self.textfiledBlock)

    {

        self.textfiledBlock(textField.text);

    }


A视图接受参数

 SecondViewController *second=[[SecondViewController alloc]init];

    UITextField *textField=(UITextField*)[self.view viewWithTag:100];

  second.textfiledBlock=^(NSString *str)

    {

        textField.text=str;

    };




3.通知


B视图切换到A视图,并且携带参数


那么B视图是发送通知的一方

//创建我们的通知

-(void)createNoti

{

    //第一个参数是通知用到名字

    //第二个接收对象,nil群发

    //第三个用户信息,我们要传的数据

    

    UITextField *textField=(UITextField*)[self.view viewWithTag:12];

    NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:textField.text,@"textFieldStr", nil];

    //发送通知

    [[NSNotificationCenter defaultCenter] postNotificationName:@"SecondFieldStr" object:nil userInfo:dict];

    


}

A视图接受通知的一方

  //用户通知中心

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


//接收通知的方法

-(void)reciveFieldStr:(NSNotification*)notification

{

    //NSLog(@"收到通知");

    

    UITextField *textField=(UITextField*)[self.view viewWithTag:12];

    

    NSDictionary *dict=notification.userInfo;

    

    textField.text=[dict objectForKey:@"textFieldStr"];

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值