iOS协议传值与Block传值
来源:互联网 发布:苹果手机淘宝没有了 编辑:程序博客网 时间:2024/06/08 11:25
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
- iOS协议传值与Block传值
- BLOCK传值 协议传值
- 最简单的IOS Block与协议
- Block传值与协议传值的比较
- block与协议-代理
- iOS开发:界面之间传值之Block传值(Block高级用法:Block传值)
- iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)
- iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)
- iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)
- IOS开发: 使用Block在两个界面之间传值(Block高级用法:Block传值)
- iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)
- iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)
- 最简单的IOS Block与协议 block是一个数据类型
- 最简单的IOS Block与协议 block作为函数参数的使用
- iOS传值之block传值(一)
- iOS传值之block传值(一)
- iOS传值二block传值,通知传值
- ios控制器之间传值-Block传值
- 几何画板构造象限的平分线的方法
- shell 中 here documemt << 与 <<- 的区别
- STL——迭代器失效
- C++编码规范
- js 笔记
- iOS协议传值与Block传值
- Java Date获取 年月日时分秒
- P处理自动修改办公IP(附源码以及学习文件)
- iOS 越狱的Tweak开发
- 词典
- iOS在线查看手机UDID
- LINQ的左连接、右连接、内连接
- VC++ 属性配置
- mysql 函数调用或建立时错误