iOS阶段学习第32天笔记(页面传值方法介绍)
iOS学习(UI)知识点整理
一、界面传值方法
1、方法一 Block传值 通过SubView视图的Block向View视图传值改变View视图的背景色 实例代码:
1)SubViewController.h 文件的代码实现
#import <UIKit/UIKit.h>
@interface SubViewController : UIViewController
@property (nonatomic,copy) void(^callback)(UIColor *color);
-(void)login:(NSString *)username password:(NSString *)password complete:(void(^)(UIColor *color))callback;
@end
2)SubViewController.m 文件代码实现
#import "SubViewController.h"
@interface SubViewController ()
@end
@implementation SubViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self
action:@selector(testAciton)];
}
-(void)testAciton
{
_callback([UIColor blueColor]);
[self.navigationController popViewControllerAnimated:YES];
}
-(void)login:(NSString *)username password:(NSString *)password complete:(void(^)(UIColor *color))callback
{
callback([UIColor redColor]);
}
@end
3)ViewController.m 文件的代码实现:
#import "ViewController.h"
#import "SubViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks
target:self action:@selector(testAciton)];
}
-(void)testAciton
{
SubViewController *subvc = [[SubViewController alloc]init];
// subvc.callback --> void(^)(UIColor *)
//传参与回调
[subvc login:@"Admin" password:@"" complete:^(UIColor *color) {
self.view.backgroundColor = color;
}];
[subvc setCallback:^(UIColor *color) {
self.view.backgroundColor = color;
}];
[self.navigationController pushViewController:subvc animated:YES];
}
@end
2、方法二 通知中心传值 实例代码:
1)添加一个通知中心管理类 KKNotificationCenter.h 文件实现代码
#import <Foundation/Foundation.h>
@interface KKNotification : NSObject
//观察者对象
@property (nonatomic,strong) id observer;
//观察者通知执行方法
@property (nonatomic,assign) SEL selector;
//观察者的键值
@property (nonatomic,copy) NSString *name;
//返回参数
@property (nonatomic,strong) id object;
@end @interface KKNotificationCenter : NSObject
+(id)defaultCenter;
-(void)addObserver:(id)observer selector:(SEL)sel name:(NSString *)name object:(id)object;
-(void)postNotificationName:(NSString *)name object:(id)object;
@end
2)KKNotificationCenter.m 文件实现代码
#import "KKNotificationCenter.h"
@implementation KKNotification
@end
@implementation KKNotificationCenter
{
NSMutableArray *_notArray;
} +(id)defaultCenter
{
static KKNotificationCenter *_q = nil;
if (!_q) {
_q = [[KKNotificationCenter alloc]init];
}
return _q;
}
- (id)init
{
self = [super init];
if (self) {
_notArray = [[NSMutableArray alloc]init];
}
return self;
} -(void)addObserver:(id)observer selector:(SEL)sel name:(NSString *)name object:(id)object
{
KKNotification *not = [[KKNotification alloc]init];
not.observer = observer;
not.selector = sel;
not.name = name;
[_notArray addObject:not];
} -(void)postNotificationName:(NSString *)name object:(id)object
{
for (KKNotification *not in _notArray) {
if ([not.name isEqualToString:name]) {
not.object = object;
[not.observer performSelector:not.selector withObject:not];
}
}
}
@end
3)在ViewController.m 视图文件中注册一个观察者 实例代码
#import "ViewController.h"
#import "SubViewController.h"
#import "KKNotificationCenter.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self
action:@selector(testAciton)]; //在通知中心里注册一个观察者:self
//name:作为观察者的键值
[[KKNotificationCenter defaultCenter]addObserver:self selector:@selector(notAction:)
name:@"changeColor" object:nil];
} -(void)notAction:(KKNotification *)not
{
self.view.backgroundColor = not.object;
} -(void)testAciton
{
SubViewController *subvc = [[SubViewController alloc]init];
[subvc setValue:@ forKey:@"age"];
[subvc performSelector:@selector(testAciton) withObject:nil];
[self.navigationController pushViewController:subvc animated:YES];
}
@end
4)SubViewController.h 文件中的代码实现
#import <UIKit/UIKit.h>
@interface SubViewController : UIViewController
{
@private
int age;
}
-(void)test;
@end
5)SubViewController.m 文件中执行通知的代码实现
#import "SubViewController.h"
#import "KKNotificationCenter.h"
@interface SubViewController ()
@end
@implementation SubViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks
target:self action:@selector(testAciton)];
}
-(void)testAciton
{
//通知中心广播
//广播使用的名字,必须和注册时,使用的名字一致
[[KKNotificationCenter defaultCenter]postNotificationName:@"changeColor" object:[UIColor redColor]];
[self.navigationController popViewControllerAnimated:YES];
}
-(void)test
{
NSLog(@"%d",age);
}
@end
3、方法三 Target-Action 传值 通过SubViewController传值改变ViewControllerd的背景色 实例代码
1)SubViewController.h 文件中的代码实现
#import <UIKit/UIKit.h>
//枚举 区别控制器类型
typedef NS_ENUM(NSUInteger, PlayerController) {
PlayerControllerLeft,
PlayerControllerRight,
PlayerControllerCenter,
PlayerControllerUp,
PlayerControllerDown,
}; @interface SubViewController : UIViewController
-(void)addTarget:(id)obj action:(SEL)action forEvents:(PlayerController)events;
@end
2)SubViewController.m 文件中的代码实现
#import "SubViewController.h"
@interface SubViewController ()
{
id _leftObj;
id _rightObj;
SEL _leftSelector;
SEL _rightSelector;
}
@end
@implementation SubViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:@selector(leftAction)];
} -(void)addTarget:(id)obj action:(SEL)action forEvents:(PlayerController)events
{
switch (events) {
case PlayerControllerLeft:
{
_leftObj = obj;
_leftSelector = action;
}
break;
case PlayerControllerRight:
{
_rightObj = obj;
_rightSelector = action;
}
break;
default:
break;
}
}
-(void)leftAction
{
[_leftObj performSelector:_leftSelector withObject:nil];
[self.navigationController popViewControllerAnimated:YES];
}
@end
3)ViewController.m 文件中的代码实现
#import "ViewController.h"
#import "SubViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *btn
= [UIButton buttonWithType:UIButtonTypeCustom];
[btn addTarget:self action:@selector(clickAction) forControlEvents:UIControlEventTouchUpInside];
btn.frame = CGRectMake(, , , );
btn.backgroundColor = [UIColor blueColor];
[self.view addSubview:btn];
} -(void)clickAction
{
SubViewController *subvc = [[SubViewController alloc]init];
[subvc addTarget:self action:@selector(leftAction) forEvents:PlayerControllerLeft];
[subvc addTarget:self action:@selector(rightAction) forEvents:PlayerControllerRight];
[self.navigationController pushViewController:subvc animated:YES];
}
-(void)leftAction
{
self.view.backgroundColor = [UIColor yellowColor];
}
-(void)rightAction
{
self.view.backgroundColor = [UIColor redColor];
}
@end
4、方法四 代理传值 通过SubViewController传值改变ViewControllerd的背景色 实例代码
1) SubViewController.h文件中的代码实现
#import <UIKit/UIKit.h>
//协议的本质就是一个方法列表
@protocol Protocal <NSObject>
-(void)changeTitle:(NSString *)title;
-(void)changeBackColor:(UIColor *)color;
-(void)changeViewColor:(UIColor *)color;
@end
@interface SubViewController : UIViewController
@property (nonatomic,assign) id<Protocal> delegate;
@end
2)SubViewController.m文件中的代码实现
#import "SubViewController.h"
#import "ViewController.h"
@interface SubViewController ()
@end
@implementation SubViewController - (void)viewDidLoad {
[super viewDidLoad];
self.title = @"界面二";
self.view.backgroundColor = [UIColor grayColor];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks
target:self action:@selector(backAction)];
} -(void)backAction
{
// NSArray *arr = self.navigationController.viewControllers;
// UIViewController *vc = arr[arr.count - 2];
// ViewController *vc = (ViewController *)_delegate;
[_delegate changeTitle:@"home"];
[_delegate changeBackColor:[UIColor blueColor]];
[_delegate changeViewColor:[UIColor yellowColor]];
[self.navigationController popViewControllerAnimated:YES];
}
-(void)dealloc
{
NSLog(@"----------------------");
}
@end
3)ViewController.h文件中的代码实现
#import <UIKit/UIKit.h>
#import "SubViewController.h"
@interface ViewController : UIViewController<Protocal>
-(void)changeTitle:(NSString *)title;
-(void)changeBackColor:(UIColor *)color;
-(void)changeViewColor:(UIColor *)color;
@end
4)ViewController.m文件中的代码实现
#import "ViewController.h"
#import "SubViewController.h"
@interface ViewController ()
{
UIView *_v;
SubViewController *subvc;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:@selector(pushAction)];
self.title = @"首页";
_v = [[UIView alloc]initWithFrame:CGRectMake(, , , )];
[self.view addSubview:_v];
_v.backgroundColor = [UIColor redColor];
subvc = [[SubViewController alloc]init];
subvc.delegate = self;
}
-(void)pushAction
{
[self.navigationController pushViewController:subvc animated:YES];
}
-(void)changeViewColor:(UIColor *)color
{
_v.backgroundColor = color;
}
-(void)changeTitle:(NSString *)title
{
self.title = title;
}
-(void)changeBackColor:(UIColor *)color
{
self.view.backgroundColor = color;
}
@end
5、方法五 属性传值 通过FirstViewController传值改变SubViewControllerd的背景色 实例代码
1)SubViewController.h文件中的代码实现
#import <UIKit/UIKit.h>
@interface SubViewController : UIViewController
@property(nonatomic,strong) NSString *mtitle;
@property(nonatomic,strong) UIColor *color;
@end
2)SubViewController.m 文件中的代码实现
#import "SubViewController.h"
@interface SubViewController ()
@end
@implementation SubViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title=_mtitle;
self.view.backgroundColor=_color;
}
@end
3)FirstViewController.m 文件中的代码实现
#import "FirstViewController.h"
#import "SubViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor];
UIButton *button=[[UIButton alloc]init];
button.frame=CGRectMake(, , , );
button.backgroundColor=[UIColor blackColor];
[button setTitle:@"下一页" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button addTarget:self
action:@selector(touchButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
} -(void)touchButton:(UIButton*)button{
button.backgroundColor=[UIColor grayColor]; SubViewController *subVC=[[SubViewController alloc]init];
subVC.mtitle=@"页面二";
subVC.color=[UIColor redColor];
[self.navigationController pushViewController:subVC animated:YES];
}
@end
iOS阶段学习第32天笔记(页面传值方法介绍)的更多相关文章
- iOS阶段学习第26天笔记(UILabel的介绍)
iOS学习(UI)知识点整理 一.关于UILabel的使用介绍 1)概念:UILabel是一个继承自UIView的用于展示文本信息的控件 2)UI中所有的控件都继承自UIView 即UIView 是U ...
- iOS阶段学习第33天笔记(自定义标签栏(UITabBar)介绍)
iOS学习(UI)知识点整理 一.自定义标签栏 1.方法一 单个创建标签栏 #import "AppDelegate.h" #import "SecondViewCont ...
- iOS 阶段学习第24天笔记(Block的介绍)
iOS学习(OC语言)知识点整理 一.Block 的介绍 1)概念: block 是一种数据类型,类似于C语言中没有名字的函数,可以接收参数,也可以返回值与C函数一样被调用 封装一段代码 可以在任何地 ...
- iOS 阶段学习第23天笔记(XML数据格式介绍)
iOS学习(OC语言)知识点整理 一.XML数据格式介绍 1)概念:xml是extensible markup language扩展的标记语言,一般用来表示.传输和存储数据 2)xml与json目前使 ...
- iOS 阶段学习第22天笔记(JSON数据格式介绍)
iOS学习(OC语言)知识点整理 一.JSON数据格式 1)概念:json是一种网络数据传输格式,有值/对象:{“A”:1,”B”:”2”…}词典:对象的序列:[,,,,,]数组两种数据类型 2)UR ...
- iOS阶段学习第30天笔记( UIViewController—Delegate(代理) )
iOS学习(UI)知识点整理 一.UIViewController的介绍 1)概念:UIViewController 即视图控制器,用来管理和控制页面跳转的一个类 ,iOS里面采用了MVC的体系结构, ...
- iOS阶段学习第31天笔记(UINavigationBar介绍)
iOS学习(UI)知识点整理 一.UINavigationBar 的介绍 1)概念:UINavigationBar 是用于定义导航栏按钮的一个类对象 2)在使用UINavigationBar之前必须先 ...
- iOS阶段学习第34天笔记(UI小组件 UISegment-UISlider-UIStepper-UIProgressView-UITextView介绍)
iOS学习(UI)知识点整理 一.UI小组件 1.UISegmentedControl 分段选择器 实例代码 - (void)viewDidLoad { [super viewDidLoad]; / ...
- iOS 阶段学习第11天笔记(OC基础知识)
iOS学习(OC语言)知识点整理 一.OC基础知识 1)#import 用于导入头文件,预处理阶段加载引用,只加载一次. 2)OC 依赖于Foundation框架下的头文件Foundation.h, ...
随机推荐
- ArcGIS制作放射状流向地图(Radial Flow Map)
流向地图火了,因为Facebook的那张著名的友邻图,抑或因为<数据可视化之美>中介绍飞行模式的航线图,总之,流向地图以它特殊的可视化形式,直观地展示事物之间的联系,尤其在展示网络流向.贸 ...
- Module-Zero之租户管理
返回<Module Zero学习目录> 开启多租户 租户实体 租户管理者 默认租户 开启多租户 ABP和Module-Zero可以运行多租户或单租户模式.多租户默认是禁用的.我们可以在mo ...
- springboot之HelloWorld
简介 为了简化开发Spring的复杂度,Spring提供了SpringBoot可以快速开发一个应用,这里就简单介绍下SpringBoot如何快速开发一个J2EE应用 HelloWorld 首先在gra ...
- 浅谈微信小程序对于创业者,意味着什么?
尽管这个话题,有点儿烂大街,然而作为开发者兼创业人,兹以为很有必要为自己梳理一番. 多年前,当萌生创业的念头时,我是这样在脑海里绘制这幅蓝图的: 我需要一个域名,一个服务器,并且备了案. 我需要至少一 ...
- Andrew Ng机器学习公开课笔记 -- 学习理论
网易公开课,第9,10课 notes,http://cs229.stanford.edu/notes/cs229-notes4.pdf 这章要讨论的问题是,如何去评价和选择学习算法 Bias/va ...
- Java伪界面操作数据库的小实例
首先在Mysql中有两个表fruit和login: package com.zuoye; import java.sql.*; import java.util.*; public class Tes ...
- struts1一:基本简介
struts是开源框架.使用Struts的目的是为了帮助我们减少在运用MVC设计模型来开发Web应用的时间.如果我们想混合使用Servlets和JSP的优点来建立可扩展的应用,struts是一个不错的 ...
- WCF学习之旅—WCF第二个示例(五)
二.WCF服务端应用程序 第一步,创建WCF服务应用程序项目 打开Visual Studio 2015,在菜单上点击文件—>新建—>项目—>WCF服务应用程序.在弹出界面的“名称”对 ...
- Windows下安装python2和python3双版本
现在大家常用的桌面操作系统有:Windows.Mac OS.ubuntu,其中Mac OS 和 ubuntu上都会自带python.这里我们只介绍下Windows(我用的Win10)环境下的pytho ...
- markdown语法练习
Markdown练习 这篇文章 主要用于练习markdown各种基本语法. 这篇文章 主要用于练习markdown各种基本语法. 1.标题设置 1.通过在文字下方添加"="或者&q ...