(一)属性传值
一般用于将第一个界面的值传到第二个界面,不可从第二个界面向第一个界面传值
第一个界面代码:
#import "ViewController.h"
#import "DetailViewController.h"
@interface ViewController ()
@property(nonatomic,strong)UITextField *textNstring;//创建一个属性
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor = [UIColorbrownColor];
_textNstring = [[UITextFieldalloc]initWithFrame:CGRectMake(150,100, 80, 20)];
_textNstring.text =@"123456";
[self.viewaddSubview:self.textNstring];
//创建一个按钮,应用其点击事件,进行传值和转页
UIButton *button = [[UIButtonalloc]initWithFrame:CGRectMake(50,100, 100,20)];
[button setTitle:@"属性传值"forState:UIControlStateNormal];
[button addTarget:selfaction:@selector(click)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
}
//按钮的点击事件
- (void)click{
DetailViewController *dvc = [[DetailViewControlleralloc]init];
//属性传值
dvc.string =self.textNstring.text;
//跳转页面
[selfshowViewController:dvc sender:nil];
}
#import <UIKit/UIKit.h>
@interface DetailViewController :UIViewController
@property(nonatomic,strong)NSString *string;
@property(nonatomic,strong)UILabel *NSlable;
@end
.m文件
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor = [UIColoryellowColor];
_NSlable = [[UILabelalloc]initWithFrame:CGRectMake(100,100, 100, 30)];
_NSlable.textColor = [UIColorblackColor];
_NSlable.backgroundColor = [UIColorwhiteColor];
_NSlable.text =_string;
[self.viewaddSubview:self.NSlable];
}
(二)Block传值
一般用于将第二个界面的值,返回给上一个界面
第一个界面代码:
#import "ViewController.h"
#import "DetailViewController.h"
@interface ViewController ()
@property(nonatomic,strong)UITextField *textNstring;//创建一个属性
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor brownColor];
_textNstring = [[UITextField alloc]initWithFrame:CGRectMake(150, 100, 80, 20)];
_textNstring.text = @"123456";
[self.view addSubview:self.textNstring];
//创建一个按钮,应用其点击事件,进行传值和转页
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(50, 100, 100, 20)];
[button setTitle:@"Block传值" forState:UIControlStateNormal];
[button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
//按钮的点击事件
- (void)click{
DetailViewController *dvc = [[DetailViewController alloc]init];
//Block传值
__weaktypeof(self) weakSelf =self;
dvc.block = ^(UILabel *lable){
weakSelf.textNstring.text = lable.text;
};
//跳转页面
[self.navigationControllerpushViewController:dvc animated:YES];
}
#import <UIKit/UIKit.h>
typedef void (^Block)(UILabel *);
@interface DetailViewController :UIViewController
@property(nonatomic,copy)Block block;
@property(nonatomic,strong)UILabel *NSlable;
@end
.m文件
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
_NSlable = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 30)];
_NSlable.textColor = [UIColor blackColor];
_NSlable.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.NSlable];
self.NSlable.text = @"789456";
self.block(_NSlable);
UIButton *button = [[UIButtonalloc]initWithFrame:CGRectMake(100,80, 100, 30)];
button.backgroundColor = [UIColorredColor];
[button addTarget:self action:@selector(click)
forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
}
- (void)click{
[self.navigationControllerpopViewControllerAnimated:YES];//返回上一页
}
(三)代理传值(协议传值)
第一个界面代码:
.h文件
@property(nonatomic,strong)UITextField *textNstring;//创建一个属性
.m文件
#import "ViewController.h"
#import "DetailViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor brownColor];
_textNstring = [[UITextField alloc]initWithFrame:CGRectMake(150, 100, 80, 20)];
_textNstring.text = @"123456";
[self.view addSubview:self.textNstring];
//创建一个按钮,应用其点击事件,进行传值和转页
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(50, 100, 100, 20)];
[button setTitle:@"代理传值" forState:UIControlStateNormal];
[button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
//按钮的点击事件
- (void)click{
DetailViewController *dvc = [[DetailViewController alloc]init];
dvc.delegate =self;
//跳转页面
[self.navigationController pushViewController:dvc animated:YES];
}
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController
@property(nonatomic,strong)ViewController *delegate;
@property(nonatomic,strong)UILabel *NSlable;
@end
.m文件
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
_NSlable = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 30)];
_NSlable.textColor = [UIColor blackColor];
_NSlable.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.NSlable];
self.NSlable.text = @"789456";
self.block(_NSlable);
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 80, 100, 30)];
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(click)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)click{
self.delegate.textNstring.text = self.NSlable.text ;
[self.navigationController popViewControllerAnimated:YES];//返回上一页
}
(四)单例传值
任何页面都可以调用,贯穿整个应用程序,我用的一个动画效果的单例为例子。
#import <UIKit/UIKit.h>
@interface AnimationView: UIView
+ (void)startAnimation;
+ (void)stopAnimation;
@end
#import "AnimationView.h"
@interface AnimationView ()
@property(nonnull,nonatomic,strong)UIImageView *imanageView;
@end
@implementation AnimationView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setInterfaceAnimation];
}
return self;
}
- (void)setInterfaceAnimation{
_imanageView = [[UIImageView alloc]initWithFrame: CGRectMake(self.bounds.size.width / 2 - 125, self.bounds.size.height / 2 - 50,250,80)];
_imanageView.alpha = 0.5;
[self addSubview:_imanageView];
//图片帧动画
NSArray * array = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16"];//图片名
NSMutableArray *imanges = [NSMutableArray array];
for (NSString *name in array ) {
[imanges addObject:[UIImage imageNamed:name]];//分别添加每一个名字对应的image
}
_imanageView.animationImages = imanges;//放入多张图片
_imanageView.animationDuration = 1;//转化延迟时间
_imanageView.animationRepeatCount = -1;//不结束
[[UIApplication sharedApplication].keyWindow addSubview:self.imanageView];
}
+ (void)startAnimation{
[_imanageView startAnimating];
}
+ (void)stopAnimation{
[_imanageView stopAnimating];
[_imanageView removeFromSuperview];
}
#import "AnimationView.h"
@property(nonatomic,strong)AnimationView *AnimationVC;
//离开界面的时候调用单例中的stopAnimation方法
- (void)viewDidDisappear:(BOOL)animated{
[_AnimationVC stopAnimation];
}
//载入时调用单例中的startAnimation方法
- (void)viewDidLoad {
[super viewDidLoad];
self.AnimationVC = [[AnimationView alloc]initWithFrame:self.view.bounds];
[self.AnimationVC startAnimation];
}
(五)通知传值
第一个界面代码:
#import "ViewController.h"
#import "DetailViewController.h"
@interface ViewController ()
@property(nonatomic,strong)UITextField *textNstring;//创建一个属性
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor brownColor];
_textNstring = [[UITextField alloc]initWithFrame:CGRectMake(150, 100, 80, 20)];
_textNstring.text = @"123456";
[self.view addSubview:self.textNstring];
//创建一个按钮,应用其点击事件,进行传值和转页
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(50, 100, 100, 20)];
[button setTitle:@"通知传值" forState:UIControlStateNormal];
[button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
//按钮的点击事件
- (void)click{
//注册通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(action_Notification:) name:@"DailiyRecommendNoti" object:nil];
DetailViewController *dvc = [[DetailViewController alloc]init];
//跳转页面
[self.navigationControllerpushViewController:dvc animated:YES];
}
//通知传值,获得值
- (void)action_Notification:(NSNotification *)sender{
_textNstring.text = sender.object;
}
//释放通知
- (void)dealloc{
[[NSNotificationCenter defaultCenter ]removeObserver:self];
}
#import <UIKit/UIKit.h>
@interface DetailViewController :UIViewController
@property(nonatomic,strong)UILabel *NSlable;
@end
.m文件
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
_NSlable = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 30)];
_NSlable.textColor = [UIColor blackColor];
_NSlable.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.NSlable];
self.NSlable.text = @"789456";
UIButton *button = [[UIButtonalloc]initWithFrame:CGRectMake(100,80, 100, 30)];
button.backgroundColor = [UIColorredColor];
[button addTarget:self action:@selector(click)
forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
}
- (void)click{
[[NSNotificationCenter defaultCenter] postNotificationName:@"DailiyRecommendNoti" object:_NSlable.text];
[self.navigationControllerpopViewControllerAnimated:YES];//返回上一页
}
(六)方法传值
第一个界面代码:
#import "ViewController.h"
#import "DetailViewController.h"
@interface ViewController ()
@property(nonatomic,strong)UITextField *textNstring;//创建一个属性
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor = [UIColorbrownColor];
_textNstring = [[UITextFieldalloc]initWithFrame:CGRectMake(150,100, 80, 20)];
_textNstring.text =@"123456";
[self.viewaddSubview:self.textNstring];
//创建一个按钮,应用其点击事件,进行传值和转页
UIButton *button = [[UIButtonalloc]initWithFrame:CGRectMake(50,100, 100,20)];
[button setTitle:@"方法传值"forState:UIControlStateNormal];
[button addTarget:selfaction:@selector(click)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
}
//按钮的点击事件
- (void)click{
DetailViewController *dvc = [[DetailViewController alloc]initWithString:_textNstring.text];
//跳转页面
[selfshowViewController:dvc sender:nil];
}
#import <UIKit/UIKit.h>
@interface DetailViewController :UIViewController
- (instancetype)initWithString:(NSString *)string;
@property(nonatomic,strong)UILabel *NSlable;
@end
.m文件
- (instancetype)initWithString:(NSString *)string{
self = [super init];
if (self) {
_NSlable = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 30)];
_NSlable.textColor = [UIColor blackColor];
_NSlable.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.NSlable];
_NSlable.text = string;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];