本节学习内容:
1.多界面传值的基本概念
2.多界面传值的方法
3.多界面传值的应用
【多界面传值 属性】
changeColor:改变颜色协义函数
delegate:代理对象设计
assign:代理赋值类型
1.创建三视图分别为VCFirst,VCSecond,VCThird,都继承于UIViewController
【VCFIrst.m】
#import"VCFirst.h"
#import"VCSencend.h"
@interface VCFirst()
@end
@implementation VCFirst
-(void)viewDidLoad{
[super viewDidLoad];
}
//点击时推出视图控制器二
-(void)touchesBegan:(NSSet
VCSecond* vc=[[VCSecond alloc]init];
vc.view.backgroundColor=[UIColor orangeColor];
//推出视图控制器二
[self.navigationController pushViewController:vc animated:YES];
}
【VCSecond.h】
//定义代理协义,视图控制器二的协义
@protocol VCSecondDelegate
//定义一个协义函数,改变背景颜色
-(void) changColor:(UIColor*)color;
@end
@interface VCSecond:UIViewController
@property(assign,nonatomic)NSInteger tag;
//定义一个代理对象,代理对象会执行协议函数,通过代理对象实现协义函数,达到代理对象改变本身属性的目的,代理对象一定要实现代理协义
@property(assign,nonatomic)id
@end
【VCSecond.m】
#import"VCSecond.h"
@interface VCSecond()
@end
@implementation VCSecond
-(void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor=[UIColor purpleColor];
//必变颜色导般栏按钮
UIBarButtonItem* btnChange=[UIBarButonItem alloc]initWithTitle:@"" style:UIBarButtonItemStyleDone target:self action:@selector(pressChange)];
self.navigationItem.rightBarButtonItem=btnChange;
}
//点击按钮触发代理事件
-(void) pressChange{
//代理对象调用事件函数
_delegate changeColor:[UIColor redColor]];]
}
【VCFirst.h】
#import
#import"VCSecond.h"
//继承 VCSecondDelegate代理
@interface VCFirst:UIViewController
-(void)changeColor:(UIColor *)color;
@end
【VCFirst.m】
#import"VCFirst.h"
@inteface VCFirst()
@end
@implementation VCFirst
-(void)viewdidLoad{
[super viewdidLoad];
}
-(void)chanteColor:(UIColor *)color{
self.view.backgroundColor=color;
}
//推出视图控制器二
-(void)touchesBegan:(NSSet
VCSecond* vc=[[VCSecond alloc]init];
//将当前的控制器做为代理对象赋值
vc.delegate=self;
//推出视图控制器二
[self.navigationController pushViewController:vc animated:YES];
}
【Delegate.m】
#import"AppDelegate.h"
#import"VCFirst.h"
#import"VCThird.h"
@interface AppDelegate()
@end
@implementation AppDelegate
-(BOOL)application:(UIApplication *)application didFinshLaunchingWithOptions:(NSDictionary *)launchOptions{
self.window=[[UIwindow alloc]initWithFrame:[UIScreen mainScreen]..bouns]
[self.window makeKeyAndVisible];
VCFirst* vcFirst=[[VCFirst alloc]init];
vcFirst* vcFirst=[[VCFirst alloc]init];
vcFirst.title=@"视图一";
vcFirst.view.backgroundColor=[UIColor whiteColor];
//加载到导航控制器
UINavigationController* nav=[[UINavigationController alloc]initWithRootViewController:vcFirst];
VCThird* vcThird=[[VCThird alloc]init];
VCThird.title=@"视图三";
vcThird.view.backgroundColor=[UIClolor greenColor];
NSArray* array=[NSArray arrayWithObjects:nav,vcThird,nil];
//分栏视图控制器
UITablBarController* tabVC=[[UITabBarController alloc]init];
tabVC.viewControllers=array;
self.window.rootViewController=tabVC;
return YES;
}