IOS(UI)_委托传值
来源:互联网 发布:mysql自增id 编辑:程序博客网 时间:2024/04/28 00:24
ViewController.m
#import "ViewController.h"#import "NextViewController.h"@interface ViewController ()<NextProtocol>{ UILabel *_label;}@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.title = @"ViewController"; _label = [UILabel new]; _label.text = @"LOL"; _label.backgroundColor = [UIColor yellowColor]; _label.textAlignment = NSTextAlignmentCenter; _label.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:_label]; NSLayoutConstraint *constraint1 = [NSLayoutConstraint constraintWithItem:_label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:100]; [self.view addConstraint:constraint1]; NSLayoutConstraint *constraint2 = [NSLayoutConstraint constraintWithItem:_label attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]; [self.view addConstraint:constraint2]; NSLayoutConstraint *constraint3 = [NSLayoutConstraint constraintWithItem:_label attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1 constant:200]; [self.view addConstraint:constraint3]; NSLayoutConstraint *constraint4 = [NSLayoutConstraint constraintWithItem:_label attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1 constant:30]; [self.view addConstraint:constraint4]; UIButton *pushButton = [UIButton buttonWithType:UIButtonTypeCustom]; [pushButton setTitle:@"push" forState:UIControlStateNormal]; [pushButton setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal]; pushButton.translatesAutoresizingMaskIntoConstraints = NO; [pushButton addTarget:self action:@selector(pushBtnAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:pushButton]; NSLayoutConstraint *constraint11 = [NSLayoutConstraint constraintWithItem:pushButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_label attribute:NSLayoutAttributeBottom multiplier:1 constant:50]; [self.view addConstraint:constraint11]; NSLayoutConstraint *constraint12 = [NSLayoutConstraint constraintWithItem:pushButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]; [self.view addConstraint:constraint12]; NSLayoutConstraint *constraint13 = [NSLayoutConstraint constraintWithItem:pushButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1 constant:100]; [self.view addConstraint:constraint13]; NSLayoutConstraint *constraint14 = [NSLayoutConstraint constraintWithItem:pushButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1 constant:30]; [self.view addConstraint:constraint14];}#pragma mark ----------------------(void)pushBtnAction:(UIButton *)sender{ NextViewController *nextViewController = [[NextViewController alloc]init]; // nextViewController.text = _label.text; [self.navigationController pushViewController:nextViewController animated:YES]; //=====让ViewController遵循协议 nextViewController.delegate = self;}//调用协议里面的方法-(void)transferString:(NSString *)string{ _label.text = string;}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end/* 从A页面推到B页面,然后B页面要回传值给A页面。 若使用委托传值的方式。那么首先需要在定义一个协议,然后在B页面定义一个循序该协议的代理的属性 判断代理是否存在并且代理是否循序协议。若存在,再调用协议里面的方法。 在A页面,让A页面遵循该协议,让后让B页面的对象的代理 = self 最后再实现一些协议里面的方法,并以此获得回传的参数 */
NextViewController.h
#import <UIKit/UIKit.h>//委托模式在UI中属于回调传值=============@protocol NextProtocol <NSObject>//在委托里面定义一个方法=============-(void)transferString:(NSString *)string;@end@interface NextViewController : UIViewController@property(nonatomic,strong)NSString *text;//定义一个协议 =======@property (nonatomic,assign)id<NextProtocol>delegate;@end
NextViewController.m
#import "NextViewController.h"//调用TextField API里面的协议 用来取消第一响应者@interface NextViewController ()<UITextFieldDelegate>@end@implementation NextViewController- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.title = @"NextViewController"; UILabel *label = [UILabel new]; label.backgroundColor = [UIColor yellowColor]; label.text = self.text; label.textAlignment = NSTextAlignmentCenter; label.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:label]; NSLayoutConstraint *constraint1 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:100]; [self.view addConstraint:constraint1]; NSLayoutConstraint *constraint2 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]; [self.view addConstraint:constraint2]; NSLayoutConstraint *constraint3 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1 constant:200]; [self.view addConstraint:constraint3]; NSLayoutConstraint *constraint4 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1 constant:30]; [self.view addConstraint:constraint4]; UITextField *textField = [[UITextField alloc]init]; textField.layer.borderColor = [UIColor lightGrayColor].CGColor; textField.layer.borderWidth = 1.0; textField.layer.cornerRadius = 5.0; textField.layer.masksToBounds = YES; //让谁遵循委托============== textField.delegate = self; textField.placeholder = @"请输入要传的值.."; textField.tag = 2000; textField.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:textField]; NSLayoutConstraint *constraint11 = [NSLayoutConstraint constraintWithItem:textField attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]; [self.view addConstraint:constraint11]; NSLayoutConstraint *constraint12 = [NSLayoutConstraint constraintWithItem:textField attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:300]; [self.view addConstraint:constraint12]; NSLayoutConstraint *constraint13 = [NSLayoutConstraint constraintWithItem:textField attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1 constant:200]; [self.view addConstraint:constraint13]; NSLayoutConstraint *constraint14 = [NSLayoutConstraint constraintWithItem:textField attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1 constant:30]; [self.view addConstraint:constraint14]; UIButton *pushButton = [UIButton buttonWithType:UIButtonTypeCustom]; [pushButton setTitle:@"back" forState:UIControlStateNormal]; [pushButton setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];//为按钮添加点击事件 [pushButton addTarget:self action:@selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside]; pushButton.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:pushButton]; NSLayoutConstraint *constraint21 = [NSLayoutConstraint constraintWithItem:pushButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]; [self.view addConstraint:constraint21]; NSLayoutConstraint *constraint22 = [NSLayoutConstraint constraintWithItem:pushButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:360]; [self.view addConstraint:constraint22]; NSLayoutConstraint *constraint23 = [NSLayoutConstraint constraintWithItem:pushButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1 constant:60]; [self.view addConstraint:constraint23]; NSLayoutConstraint *constraint24 = [NSLayoutConstraint constraintWithItem:pushButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1 constant:30]; [self.view addConstraint:constraint24];}#pragma mark ------------------(void)backBtnAction:(UIButton *)sender{ UITextField *textField = (UITextField *)[self.view viewWithTag:2000]; //判断textFiel里面的内容是否为空 if (textField.text.length == 0) { NSLog(@"请输入回传值,亲"); UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入回传值,亲" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]; [alertController addAction:action];// UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {// // }];// [alertController addAction:action1];// // UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {// // }];// [alertController addAction:action2]; [self presentViewController:alertController animated:YES completion:^{ }]; } else { //判断是否遵循协议===================== if (self.delegate && [self.delegate conformsToProtocol:@protocol(NextProtocol)]) { //遵循协议之后 调用协议里面的方法把text的值传过去 [self.delegate transferString:textField.text]; } //返回跳转的UIViewController [self.navigationController popViewControllerAnimated:YES]; }}#pragma mark ---------UITextFieldDelegate(当按键盘return键是取消第一响应者)---------- (BOOL)textFieldShouldReturn:(UITextField *)textField{ // [textField resignFirstResponder]; return YES;}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
0 0
- IOS(UI)_委托传值
- iOS笔记UI--委托代理传值
- IOS(UI)_属性传值
- IOS(UI)_手势1
- iOS代理传值(委托传值)
- ios使用委托传值问题。
- IOS(UI)_模态推送
- IOS(UI)_手势2(简单画板)
- 委托_多播委托
- IOS 页面属性传值、委托传值、单例传值
- IOS 页面属性传值、委托传值、单例传值
- UI 07 _ 导航视图控制器 与 属性传值
- iOS UIViewController之间的传值方式之一:委托代理
- iOS中的属性传值和委托模式
- IOS 委托代理(delegate)实现页面传值
- IOS 委托代理(delegate)实现页面传值
- IOS 委托代理(delegate)实现页面传值
- iOS 委托代理传值与Block回传值.
- 高等数学:第二章 导数与微分(1)导数 求导法则 反函数 复合函数
- ABAP EXCEL文件读取函数 TEXT_CONVERT_XLS_TO_SAP的使用
- 去掉Eclipse中的Validating
- 干掉头疼的finished with non-zero exit value 2
- VMware虚拟机无法安装win7系统解决办法
- IOS(UI)_委托传值
- Mac下使用钥匙串申请CSR(.certSigningRequest)
- Android漂亮的UI库集合
- Egret 游戏引擎(HTML5)
- Android 几种获得view的宽和高的方法
- 在Debian系统中iptables规则的持久化如何操作
- 高等数学:第二章 导数与微分(2)初等函数 高级导数 隐函数 参数函数
- 如何减轻跑步时的痛苦
- FAQ 前端面试大杂烩2