iOS协议传值与Block传值
#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