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];
[self.navigationController pushViewController:nextViewController animated:YES];
nextViewController.delegate = self;
}
-(void)transferString:(NSString *)string
{
_label.text = string;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
NextViewController.h
#import <UIKit/UIKit.h>
@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"
@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];
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];
[self presentViewController:alertController animated:YES completion:^{
}];
}
else
{
if (self.delegate && [self.delegate conformsToProtocol:@protocol(NextProtocol)])
{
[self.delegate transferString:textField.text];
}
[self.navigationController popViewControllerAnimated:YES];
}
}
#pragma mark ---------UITextFieldDelegate(当按键盘return键是取消第一响应者)---------
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
![这里写图片描述](https://img-blog.csdn.net/20160301141839746)