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, ...
随机推荐
- Mac OS X上IntelliJ IDEA 13与Tomcat 8的Java Web开发环境搭建
这标题实在有点拗口,不知道怎么写好,但看了标题也就明白文本的内容.最近几天在折腾这些玩意儿,所以写写总结.除了环境搭建,本文还是一篇入门级的上手教程. 去下载一些东西 JDK安装 Tomcat安装 T ...
- 自己动手写文件查找,字符串查找,查询jar包等工具
文件查找——搜索当前目录下的文件 知道大概的文件名称,使用 findf FileName findf.py import argparse, re, os from os.path import jo ...
- SQL Agent服务无法启动如何破
问题现象 从阿里云上镜像过来的一台的数据库服务器,SQL Agent服务启动不了,提示服务启动后停止.(原数据库服务器是正常的,怀疑跟镜像有关) 如下是系统日志和SQL Agent的日志 SQLSer ...
- 开始研究web,mark一下
之前想要搞引擎,经过思考之后,定位为webgl方面的引擎,这个决定早就做了,只是没有写下来 做了一些调研之后,确定使用babylon.js 和typescript 和c# 来开发 Babylo ...
- 学习scala03 控制结构
scala拥有非常宽松的控制结构. if与while scala中的if和while几乎和java中的结构一模一样. //if语句 val a= ){ println(“”) }else{ print ...
- 使用HTML5的History API
HTML5 History API提供了一种功能,能让开发人员在不刷新整个页面的情况下修改站点的URL.这个功能很有用,例如通过一段JavaScript代码局部加载页面的内容,你希望通过改变当前页面的 ...
- JS面向对象(2) -- this的使用,对象之间的赋值,for...in语句,delete使用,成员方法,json对象的使用,prototype的使用,原型继承与原型链
相关链接: JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式 JS面向对象(2) -- this的使用,对 ...
- MongoDB 内嵌文档
MongoDB是文档型的数据库系统,doc是MongoDB的数据单位,每个doc相当于关系型数据库的数据行(row),doc和row的区别在于field的原子性:row中的column是不和分割的原子 ...
- html5的audio在safari(windows)中无效
因为mac下的safari不会有这样的问题(OSX默认都装的有QuickTime),而windows下用safari的比例实在小不用考虑. apple算是偷了一个小懒.而所谓的需要quicktime并 ...
- javascript运动系列第八篇——碰壁运动
× 目录 [1]匀速碰壁 [2]自由落体 [3]投掷碰壁[4]拖拽碰壁 前面的话 碰撞运动可能是运动系列里面比较复杂的运动了.碰撞可以分为碰壁和互碰两种形式,而碰撞前后的运动形式也可以分为变速和匀速两 ...