属性传值
定义
属性传值是正向传值,只可以从前面一个页面传递到第二个页面,不可以从第二个页面传递到第一个页面。
实现
首先我们需要创建两个ViewController,在第二个界面中定义一个属性来接收第一个界面传过来的信息,然后在第一个界面中创建button的点击事件,并在这里进行传值。
代码实现
首先在ViewController.h文件中,定义属性。
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property(nonatomic , strong)UITableView* tableView;
@property UITextField* nameTextFiled;
@end
ViewController.m文件中建立按钮、对应的按钮事件以及文本框
#import "ViewController.h"
#import "ViewControllerMy.h"
@interface ViewController ()<UITextFieldDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIButton* nextbutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.view addSubview:nextbutton];
[nextbutton addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
nextbutton.frame = CGRectMake(160, 500, 70, 30);
nextbutton.backgroundColor = [UIColor blackColor];
nextbutton.tintColor = [UIColor whiteColor];
[nextbutton setTitle:@"next" forState:UIControlStateNormal];
_nameTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(40, 330, 330, 50)];
_nameTextFiled.layer.borderWidth = 2;
_nameTextFiled.layer.cornerRadius = 10;
_nameTextFiled.delegate = self;
_nameTextFiled.borderStyle = UIButtonTypeRoundedRect;
[self.view addSubview:_nameTextFiled];
}
-(void) next{
ViewControllerMy* viewcontrollerMy = [[ViewControllerMy alloc] init];
viewcontrollerMy.nameStr = _nameTextFiled.text;
viewcontrollerMy.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:viewcontrollerMy animated:YES completion:nil];
}
@end
ViewControllerMy.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface ViewControllerMy : UIViewController
@property NSString* nameStr;
@end
NS_ASSUME_NONNULL_END
在ViewController.m文件中建立返回按钮、队形赢的按钮事件,以及传值。
#import "ViewControllerMy.h"
@interface ViewControllerMy ()
@end
@implementation ViewControllerMy
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIButton* buttonback = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.view addSubview:buttonback];
[buttonback addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
buttonback.frame = CGRectMake(160, 500, 70, 30);
buttonback.backgroundColor = [UIColor whiteColor];
[buttonback setTitle:@"back" forState:UIControlStateNormal];
UILabel* LableName = [[UILabel alloc] initWithFrame:CGRectMake(180, 300, 80, 20)];
[self.view addSubview:LableName];
LableName.text = _nameStr;
}
-(void)back{
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
效果
当我们按下next时,我们可以看见:
说明文本内容已经从第一个界面传到了第二个界面
协议传值
定义
和属性传值不一样,协议传值是一种反向传值,顾名思义就是从第二个页面传入第一个页面。
实现
- 首先协议传值,就肯定与协议相关联,又因为是从第二个界面给第一个界面传值,所以就必须在第二个界面定义一个协议,这个协议中定义一个传值的方法,并在第二界面中定义一个遵守该协议的属性
- 在第一个界面定义一个代理协议
- 在一个界面初始化第二个界面时,设置代理对象
- 在第一个界面中在实现协议传值方法
- 在第二个界面中通过代理传值
代码
在MyViewController.h中
#import "ViewController.h"
NS_ASSUME_NONNULL_BEGIN
@protocol ReturnDelegate <NSObject>
//在协议中定义方法,实现传值
- (void) backInfo: (NSString *)nameStr;
@end
@interface MyViewController : UIViewController
@property NSString *nameStr;
@property UITextField *backTextField;
//定义遵守协议的属性
@property id <ReturnDelegate> returnDelegate;
@end
在ViewController.mm中代理协议,设置代理对象,实现协议传值的方法
```//
// ViewController.m
// 练习二
//
// Created by Yep on 2021/7/26.
//
#import "ViewController.h"
#import "ViewControllerMy.h"
@interface ViewController ()<UITextFieldDelegate, ReturnDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIButton* buttonNext = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonNext addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
buttonNext.frame = CGRectMake(160, 500, 70, 30);
buttonNext.backgroundColor = [UIColor blackColor];
buttonNext.tintColor = [UIColor whiteColor];
[buttonNext setTitle:@"NEXT" forState:UIControlStateNormal];
[self.view addSubview:buttonNext];
_nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(40, 330, 330, 50)];
_nameTextField.layer.borderWidth = 2;
_nameTextField.layer.cornerRadius = 10;
_nameTextField.delegate = self;
_nameTextField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:_nameTextField];
}
-(void)backinfo:(NSString*) nameStr
{
_nameTextField.text = nameStr;
}
-(void)next{
ViewControllerMy* viewcontrollerMy = [[ViewControllerMy alloc] init];
viewcontrollerMy.returnDelegate = self;
viewcontrollerMy.nameStr = _nameTextField.text;
viewcontrollerMy.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:viewcontrollerMy animated:YES completion:nil];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
}
return YES;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[_nameTextField resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
在MyViewController.m中定义返回按钮,以及对应的点击事件,并通过代理对象传值
//
// ViewControllerMy.m
// 练习二
//
// Created by Yep on 2021/7/26.
//
#import "ViewControllerMy.h"
@interface ViewControllerMy ()
@end
@implementation ViewControllerMy
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIButton* buttonBack = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonBack addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
buttonBack.frame = CGRectMake(150, 500, 70, 30);
buttonBack.backgroundColor = [UIColor blackColor];
buttonBack.tintColor = [UIColor whiteColor];
[buttonBack setTitle:@"Back" forState:UIControlStateNormal];
[self.view addSubview:buttonBack];
_backTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(40, 380, 330, 50)];
_backTextFiled.layer.borderWidth = 2;
_backTextFiled.layer.cornerRadius = 10;
_backTextFiled.delegate = self;
_backTextFiled.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:_backTextFiled];
}
-(void) back{
if([_returnDelegate respondsToSelector:@selector(backInfo:)]){
[_returnDelegate backInfo:_backTextFiled.text];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
@end