首页 > 学院 > 开发设计 > 正文

iOS阶段学习第32天笔记(页面传值方法介绍)

2019-11-14 18:25:41
字体:
来源:转载
供稿:网友

iOS学习(UI)知识点整理

一、界面传值方法  

1、方法一  Block传值  通过SubView视图的Block向View视图传值改变View视图的背景色 实例代码: 


1)SubViewController.h 文件的代码实现

1 #import <UIKit/UIKit.h>2 @interface SubViewController : UIViewController   3 @PRoperty (nonatomic,copy) void(^callback)(UIColor *color); 4 -(void)login:(NSString *)username passWord:(NSString *)password complete:(void(^)(UIColor *color))callback; 5 @end

2)SubViewController.m 文件代码实现

 1  #import "SubViewController.h"  2   @interface SubViewController ()  3   @end  4 @implementation SubViewController  5 - (void)viewDidLoad { 6     [super viewDidLoad];  7     self.view.backgroundColor = [UIColor grayColor];  8     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem  alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self 9  action:@selector(testAciton)]; 10 } 12 -(void)testAciton13 {14      _callback([UIColor blueColor]); 15     [self.navigationController popViewControllerAnimated:YES];16 }17 -(void)login:(NSString *)username password:(NSString *)password complete:(void(^)(UIColor *color))callback19 {20       callback([UIColor redColor]);21 } 22 @end

3)ViewController.m 文件的代码实现:

 1 #import "ViewController.h" 2 #import "SubViewController.h" 3 @interface ViewController () 4 @end 5 @implementation ViewController 6 - (void)viewDidLoad { 7     [super viewDidLoad];      8     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks 
target:self
action:@selector(testAciton)]; 11 }12 -(void)testAciton13 {14 SubViewController *subvc = [[SubViewController alloc]init];15 // subvc.callback --> void(^)(UIColor *) 16 //传参与回调17 [subvc login:@"Admin" password:@"123" complete:^(UIColor *color) {18 self.view.backgroundColor = color;19 }]; 21 [subvc setCallback:^(UIColor *color) {22 self.view.backgroundColor = color;23 }];24 [self.navigationController pushViewController:subvc animated:YES];25 } 26 @end

2、方法二   通知中心传值 实例代码: 

1)添加一个通知中心管理类  KKNotificationCenter.h 文件实现代码

 1 #import <Foundation/Foundation.h> 2 @interface KKNotification : NSObject 3 //观察者对象 4 @property (nonatomic,strong) id observer; 5 //观察者通知执行方法 6 @property (nonatomic,assign) SEL selector; 7 //观察者的键值 8 @property (nonatomic,copy) NSString *name; 9 //返回参数10 @property (nonatomic,strong) id object;11 @end12 13 @interface KKNotificationCenter : NSObject14 +(id)defaultCenter;15 -(void)addObserver:(id)observer selector:(SEL)sel name:(NSString *)name object:(id)object;16 -(void)postNotificationName:(NSString *)name object:(id)object;17 @end

2)KKNotificationCenter.m 文件实现代码

 1 #import "KKNotificationCenter.h" 2 @implementation KKNotification 3 @end 4 @implementation KKNotificationCenter 5 { 6     NSMutableArray *_notArray; 7 } 8  9 +(id)defaultCenter10 {11     static KKNotificationCenter *_q = nil;12     if (!_q) {13         _q = [[KKNotificationCenter alloc]init];14     }15     return _q;16 } 18 - (id)init19 {20     self = [super init];21     if (self) {22         _notArray = [[NSMutableArray alloc]init];23     }24     return self;25 }26 27 -(void)addObserver:(id)observer selector:(SEL)sel name:(NSString *)name object:(id)object28 {29    KKNotification *not = [[KKNotification alloc]init];30     not.observer = observer;31     not.selector = sel;32     not.name = name;33     [_notArray addObject:not];34 }35 36 -(void)postNotificationName:(NSString *)name object:(id)object37 {38     for (KKNotification *not in _notArray) {39         if ([not.name isEqualToString:name]) {40             not.object = object;41             [not.observer performSelector:not.selector withObject:not];42         }43     }44 }45 @end

3)在ViewController.m 视图文件中注册一个观察者 实例代码

 1 #import "ViewController.h" 2 #import "SubViewController.h" 3 #import "KKNotificationCenter.h" 4 @interface ViewController () 5 @end 6 @implementation ViewController   7 - (void)viewDidLoad { 8     [super viewDidLoad];      9     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self10  action:@selector(testAciton)];11     12 //在通知中心里注册一个观察者:self13 //name:作为观察者的键值14 [[KKNotificationCenter defaultCenter]addObserver:self selector:@selector(notAction:) 15 name:@"changeColor" object:nil];16   }17 18 -(void)notAction:(KKNotification *)not19 {20     self.view.backgroundColor = not.object;21 }22  23 -(void)testAciton24 {25     SubViewController *subvc = [[SubViewController alloc]init]; 27     [subvc setValue:@20 forKey:@"age"];28     [subvc performSelector:@selector(testAciton) withObject:nil];  30     [self.navigationController pushViewController:subvc animated:YES];31 }32 @end

4)SubViewController.h 文件中的代码实现

 1 #import <UIKit/UIKit.h> 2 @interface SubViewController : UIViewController 3 { 4 @private 5     int age; 6 }  8 -(void)test; 10 @end

5)SubViewController.m   文件中执行通知的代码实现

 1 #import "SubViewController.h" 2 #import "KKNotificationCenter.h" 3 @interface SubViewController () 4 @end 5 @implementation SubViewController 6 - (void)viewDidLoad { 7     [super viewDidLoad]; 8    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks
target:self
action:@selector(testAciton)];10 } 12 -(void)testAciton13 {14 //通知中心广播15 //广播使用的名字,必须和注册时,使用的名字一致16 [[KKNotificationCenter defaultCenter]postNotificationName:@"changeColor" object:[UIColor redColor]]; 17 [self.navigationController popViewControllerAnimated:YES];18 } 20 -(void)test21 {22 NSLog(@"%d",age);23 } 24 @end

3、方法三   Target-Action 传值 通过SubViewController传值改变ViewControllerd的背景色 实例代码

1)SubViewController.h 文件中的代码实现

 1  #import <UIKit/UIKit.h> 2 //枚举 区别控制器类型 3 typedef NS_ENUM(NSUInteger, PlayerController) { 4     PlayerControllerLeft, 5     PlayerControllerRight, 6     PlayerControllerCenter, 7     PlayerControllerUp, 8     PlayerControllerDown, 9 };10 11 @interface SubViewController : UIViewController12 -(void)addTarget:(id)obj action:(SEL)action forEvents:(PlayerController)events;13 @end

2)SubViewController.m 文件中的代码实现

 1 #import "SubViewController.h" 2 @interface SubViewController () 3 { 4     id _leftObj; 5     id _rightObj; 6     SEL _leftSelector; 7     SEL _rightSelector; 8 } 9 @end10 @implementation SubViewController11 - (void)viewDidLoad {12     [super viewDidLoad];  14     self.view.backgroundColor = [UIColor grayColor]; 15     self.navigationItem.rightBarButtonItem  = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
target:self
action:@selector(leftAction)];18 }19 20 -(void)addTarget:(id)obj action:(SEL)action forEvents:(PlayerController)events21 {22 switch (events) {23 case PlayerControllerLeft:24 {25 _leftObj = obj;26 _leftSelector = action;27 }28 break;29 case PlayerControllerRight:30 {31 _rightObj = obj;32 _rightSelector = action;33 }34 break; 36 default:37 break;38 }39 } 41 -(void)leftAction42 {43 [_leftObj performSelector:_leftSelector withObject:nil]; 45 [self.navigationController popViewControllerAnimated:YES];46 } 48 @end

3)ViewController.m 文件中的代码实现

 1 #import "ViewController.h" 2 #import "SubViewController.h" 3 @interface ViewController () 4 @end 5 @implementation ViewController 6 - (void)viewDidLoad { 7     [super viewDidLoad]; 8     UIButton *btn 9     = [UIButton buttonWithType:UIButtonTypeCustom];10     [btn addTarget:self action:@selector(clickAction) forControlEvents:UIControlEventTouchUpInside];11     btn.frame = CGRectMake(10, 100, 300, 50);12     btn.backgroundColor = [UIColor blueColor];13     [self.view addSubview:btn]; 15 }16 17 -(void)clickAction18 {19     SubViewController *subvc = [[SubViewController alloc]init]; 21     [subvc addTarget:self action:@selector(leftAction) forEvents:PlayerControllerLeft]; 23     [subvc addTarget:self action:@selector(rightAction) forEvents:PlayerControllerRight]; 25     [self.navigationController pushViewController:subvc animated:YES];26 } 28 -(void)leftAction29 {30     self.view.backgroundColor = [UIColor yellowColor];31 } 33 -(void)rightAction34 {35     self.view.backgroundColor = [UIColor redColor];36 } 38 @end

4、方法四 代理传值  通过SubViewController传值改变ViewControllerd的背景色 实例代码

1) SubViewController.h文件中的代码实现

 1 #import <UIKit/UIKit.h> 2 //协议的本质就是一个方法列表 3 @protocol Protocal <NSObject> 4 -(void)changeTitle:(NSString *)title; 5 -(void)changeBackColor:(UIColor *)color; 6 -(void)changeViewColor:(UIColor *)color; 7 @end 8 @interface SubViewController : UIViewController 9 @property (nonatomic,assign) id<Protocal> delegate;10 @end

2)SubViewController.m文件中的代码实现

 1 #import "SubViewController.h" 2 #import "ViewController.h" 3 @interface SubViewController () 4 @end 5 @implementation SubViewController 6  7 - (void)viewDidLoad { 8     [super viewDidLoad]; 9     self.title = @"界面二";10     self.view.backgroundColor = [UIColor grayColor];11     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks 
target:self
action:@selector(backAction)];13 }14 15 -(void)backAction16 {17 // NSArray *arr = self.navigationController.viewControllers; 18 // UIViewController *vc = arr[arr.count - 2]; 19 // ViewController *vc = (ViewController *)_delegate; 21 [_delegate changeTitle:@"home"]; 22 [_delegate changeBackColor:[UIColor blueColor]]; 23 [_delegate changeViewColor:[UIColor yellowColor]]; 25 [self.navigationController popViewControllerAnimated:YES]; 27 }28 -(void)dealloc29 {30 NSLog(@"----------------------");31 }32 @end

3)ViewController.h文件中的代码实现

 1 #import <UIKit/UIKit.h> 2 #import "SubViewController.h"  4 @interface ViewController : UIViewController<Protocal>  6 -(void)changeTitle:(NSString *)title; 7 -(void)changeBackColor:(UIColor *)color; 8 -(void)changeViewColor:(UIColor *)color; 10 @end

4)ViewController.m文件中的代码实现

 1 #import "ViewController.h" 2 #import "SubViewController.h"  3 @interface ViewController () 4 { 5     UIView *_v; 6     SubViewController *subvc; 7 } 8 @end 10 @implementation ViewController 12 - (void)viewDidLoad {13     [super viewDidLoad]; 14     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
target:self
action:@selector(pushAction)];17 self.title = @"首页"; 18 _v = [[UIView alloc]initWithFrame:CGRectMake(100, 200, 200, 100)];19 [self.view addSubview:_v];20 _v.backgroundColor = [UIColor redColor]; 21 subvc = [[SubViewController alloc]init];22 subvc.delegate = self; 23 }-(void)pushAction26 {27 [self.navigationController pushViewController:subvc animated:YES];28 }29-(void)changeViewColor:(UIColor *)color31 {32 _v.backgroundColor = color;33 } -(void)changeTitle:(NSString *)title36 {37 self.title = title;38 } -(void)changeBackColor:(UIColor *)color41 {42 self.view.backgroundColor = color;43 } @end

5、方法五  属性传值  通过FirstViewController传值改变SubViewControllerd的背景色 实例代码

1)SubViewController.h文件中的代码实现

1 #import <UIKit/UIKit.h> 2 @interface SubViewController : UIViewController 3 @property(nonatomic,strong) NSString *mtitle; 4 @property(nonatomic,strong) UIColor *color; 5 @end

2)SubViewController.m 文件中的代码实现

 1 #import "SubViewController.h" 2  @interface SubViewController () 3 @end 4 @implementation SubViewController 5 - (void)viewDidLoad { 6 [super viewDidLoad];  self.title=_mtitle; 9self.view.backgroundColor=_color;}12 @end

3)FirstViewController.m 文件中的代码实现

 1 #import "FirstViewController.h" 2 #import "SubViewController.h" 3 @interface FirstViewController () 4 @end 5 @implementation FirstViewController 6 - (void)viewDidLoad { 7     [super viewDidLoad]; 8     self.view.backgroundColor=[UIColor whiteColor]; 9     UIButton *button=[[UIButton alloc]init];10     button.frame=CGRectMake(80, 100, 200, 30);11     button.backgroundColor=[UIColor blackColor];12     [button setTitle:@"下一页" forState:UIControlStateNormal];13     [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];14     [button addTarget:self 15 action:@selector(touchButton:) forControlEvents:UIControlEventTouchUpInside]; 16     [self.view addSubview:button];17 }18 19 -(void)touchButton:(UIButton*)button{  20     button.backgroundColor=[UIColor grayColor];     21  22     SubViewController *subVC=[[SubViewController alloc]init];23     subVC.mtitle=@"页面二";24     subVC.color=[UIColor redColor];25     [self.navigationController pushViewController:subVC animated:YES];26 }27 @end

 

 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表