iOS协议传值与Block传值

来源:互联网 发布:苹果手机淘宝没有了 编辑:程序博客网 时间:2024/06/08 11:25

iOS协议传值与Block传值

首先是协议传值,协议的概念这里不作过多介绍,直接入主题,如何通过协议实现两个Controller的传值。
项目描述:代码实现两个界面,第一个界面上有一个label,一个button,点击button进入第二个界面。第二个界面上有一个textField,输入完字符后,点击return实现字符回传,并显示在第一个界面上的label上。

协议传值代码如下:

AppDelegat.m:

#import "AppDelegate.h"

#import "ViewController.h"

@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds];

    ViewController *vc = [ViewControllernew];

    self.window.rootViewController = vc;

    [self.windowmakeKeyAndVisible];

    

    returnYES;

}


ViewController.m:

#import "ViewController.h"

#import "TextViewController.h"

@interface ViewController ()<TextViewControllerDelegate>

@property(nonatomic,strong)UILabel *label;

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    //创建一个label

    self.label = [UILabelnew];

    self.label.backgroundColor = [UIColor redColor];

    self.label.frame =CGRectMake(200,250,100,50);

    //创建一个btn

    UIButton *btn = [UIButtonnew];

    btn.backgroundColor = [UIColororangeColor];

    btn.frame =CGRectMake(200,400,100,80);

    //添加按钮事件

    [btn addTarget:selfaction:@selector(btnclick )forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:self.label];

    [self.viewaddSubview:btn];

    



}

    //按钮事件

- (void)btnclick{

    TextViewController *vc = [[TextViewControlleralloc]init];

    vc.delegate =self;

    [selfpresentViewController:vcanimated:YEScompletion:nil];

}

#pragma TextViewControllerDelegate方法

    //代理事件

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

    self.label.text =str;

    NSLog(@"%@ %@",str,self.label.text);

}

@end


TextViewController.h

#import <UIKit/UIKit.h>


//添加协议

@protocol  TextViewControllerDelegate<NSObject>


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


@end

@interface TextViewController :UIViewController

@property(nonatomic,weak)id<TextViewControllerDelegate> delegate;


@end



TextViewController.m

#import "TextViewController.h"


@interface TextViewController ()<UITextFieldDelegate>

@property(nonatomic,strong)UITextField *textField;

@end


@implementation TextViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    //创建一个文本输入框

    _textField = [UITextFieldnew];

    _textField.backgroundColor = [UIColorgrayColor];

    _textField.frame =CGRectMake(50,50,100,100);

    _textField.delegate =self;

    [self.viewaddSubview:_textField];

}


#pragma mark --UITextFieldDelegate方法

    //监听return键点击

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

   

    NSLog(@"发送键被点击了 %@",textField.text);

     [selfsendText:textField.text];

    [selfdismissViewControllerAnimated:YEScompletion:nil];

    returnYES;

}

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

    //判断代理是否实现了代理方法

    if ([self.delegaterespondsToSelector:@selector(textViewControllerDidReturnText:)]) {

        [self.delegatetextViewControllerDidReturnText:str];

    }

}

@end



block传值代码如下:

AppDelegate.m

#import "AppDelegate.h"

#import "ViewController.h"

@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

   _window = [[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds];

    ViewController *vc = [ViewControllernew];

    vc.view.backgroundColor = [UIColorwhiteColor];

    _window.rootViewController =vc;

    [_windowmakeKeyAndVisible];

    

    returnYES;

}


ViewController.m

#import "ViewController.h"

#import "AViewController.h"

@interface ViewController ()

@property(nonatomic,strong)UILabel *label;

@property(nonatomic,strong)UIButton *button;

@end


@implementation ViewController

- (void)viewDidLoad{


    [superviewDidLoad];

    //创建一个label

     _label = [UILabelnew];

     _label.backgroundColor = [UIColorredColor];

     _label.frame =CGRectMake(200,250,100,50);

    //创建一个button

    UIButton *btn = [UIButtonnew];

    btn.backgroundColor = [UIColororangeColor];

    btn.frame =CGRectMake(200,400,100,80);

    //添加按钮点击事件

    [btn addTarget:selfaction:@selector(btnclick )forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:_label];

    [self.viewaddSubview:btn];

    

    

}

    //按钮点击事件

- (void)btnclick{

    AViewController *view = [AViewControllernew];

    view.textBlock = ^(NSString *str){

    self.label.text = str;

    NSLog(@"label的文字---------%@",self.label.text);

    };

    [selfpresentViewController:viewanimated:YEScompletion:nil];

}

@end



AViewController.h

#import <UIKit/UIKit.h>

//声明一个block

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

@interface AViewController : UIViewController

@property(nonatomic,copy)ReturnTextBlock textBlock;

@property (nonatomic,retain)UITextField *textField;

-(void)returnText:(ReturnTextBlock)block;

@end


AViewController.m

#import "AViewController.h"

@interface AViewController ()<UITextFieldDelegate>

@end


@implementation AViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    //创建一个文本输入框

    _textField = [UITextFieldnew];

    _textField.backgroundColor = [UIColorgrayColor];

    _textField.frame =CGRectMake(50,50,100,100);

    _textField.delegate =self;

    [self.viewaddSubview:_textField];

}

//在第一个界面传进来一个Block语句块的函数

//把传进来的Block语句块保存到本类的实例变量returnTextBlock.h中定义的属性)中,然后寻找一个时机调用

-(void)returnText:(ReturnTextBlock)block{

    self.textBlock = block;

}


- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    NSString *str = textField.text;

    NSLog(@"发送键被点击了%@",str);

    //调用block

    if (_textBlock) {

        

        _textBlock(str);

    }

    [selfdismissViewControllerAnimated:YEScompletion:nil];

    returnYES;

}

@end



0 0
原创粉丝点击