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天笔记(页面传值方法介绍)的更多相关文章

  1. iOS阶段学习第26天笔记(UILabel的介绍)

    iOS学习(UI)知识点整理 一.关于UILabel的使用介绍 1)概念:UILabel是一个继承自UIView的用于展示文本信息的控件 2)UI中所有的控件都继承自UIView 即UIView 是U ...

  2. iOS阶段学习第33天笔记(自定义标签栏(UITabBar)介绍)

    iOS学习(UI)知识点整理 一.自定义标签栏 1.方法一 单个创建标签栏 #import "AppDelegate.h" #import "SecondViewCont ...

  3. iOS 阶段学习第24天笔记(Block的介绍)

    iOS学习(OC语言)知识点整理 一.Block 的介绍 1)概念: block 是一种数据类型,类似于C语言中没有名字的函数,可以接收参数,也可以返回值与C函数一样被调用 封装一段代码 可以在任何地 ...

  4. iOS 阶段学习第23天笔记(XML数据格式介绍)

    iOS学习(OC语言)知识点整理 一.XML数据格式介绍 1)概念:xml是extensible markup language扩展的标记语言,一般用来表示.传输和存储数据 2)xml与json目前使 ...

  5. iOS 阶段学习第22天笔记(JSON数据格式介绍)

    iOS学习(OC语言)知识点整理 一.JSON数据格式 1)概念:json是一种网络数据传输格式,有值/对象:{“A”:1,”B”:”2”…}词典:对象的序列:[,,,,,]数组两种数据类型 2)UR ...

  6. iOS阶段学习第30天笔记( UIViewController—Delegate(代理) )

    iOS学习(UI)知识点整理 一.UIViewController的介绍 1)概念:UIViewController 即视图控制器,用来管理和控制页面跳转的一个类 ,iOS里面采用了MVC的体系结构, ...

  7. iOS阶段学习第31天笔记(UINavigationBar介绍)

    iOS学习(UI)知识点整理 一.UINavigationBar 的介绍 1)概念:UINavigationBar 是用于定义导航栏按钮的一个类对象 2)在使用UINavigationBar之前必须先 ...

  8. iOS阶段学习第34天笔记(UI小组件 UISegment-UISlider-UIStepper-UIProgressView-UITextView介绍)

    iOS学习(UI)知识点整理 一.UI小组件 1.UISegmentedControl 分段选择器  实例代码 - (void)viewDidLoad { [super viewDidLoad]; / ...

  9. iOS 阶段学习第11天笔记(OC基础知识)

    iOS学习(OC语言)知识点整理 一.OC基础知识 1)#import  用于导入头文件,预处理阶段加载引用,只加载一次. 2)OC 依赖于Foundation框架下的头文件Foundation.h, ...

随机推荐

  1. Linux环境下部署完JDK后运行一个简单的Java程序

    前言 前一篇文章详细讲解了如何在Windows环境下安装虚拟机+Linux系统,并且成功部署了JDK. 不过部署完JDK之后,我们判断部署是否成功的依据是看"java -version&qu ...

  2. 安装 Linux 时碰到的硬盘分区的陷阱及应对

    硬盘分区的陷阱及应对 之所以想到写这篇,是因为本人在折腾 Linux 系统的过程中,有多次掉入硬盘分区的陷阱的经历.最近几天,再一次掉入坑中,折腾了两天才从坑中爬出来.经过多方查询资料,终于弄明白了硬 ...

  3. android-plugmgr源代码分析

    android-plugmgr是一个Android插件加载框架,它最大的特点就是对插件不需要进行任何约束.关于这个类库的介绍见作者博客,市面上也有一些插件加载框架,但是感觉没有这个好.在这篇文章中,我 ...

  4. 可以这样去理解group by和聚合函数

    写在前面的话:用了好久group by,今天早上一觉醒来,突然感觉group by好陌生,总有个筋别不过来,为什么不能够select * from Table group by id,为什么一定不能是 ...

  5. Linux下MongoDB服务安装

    Linux下MongoDB服务安装 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案.MongoDB是一个介于关系数据库和非关系数据 ...

  6. Zookeeper API for JAVA实战与应用

    package com.zookeeper.watcher; import java.util.List; import java.util.concurrent.CountDownLatch; im ...

  7. spring快速入门(四)

    一.在spring快速入门(三)的基础上,我们来了解BeanFactory及配置. Client package com.murong.client; import org.springframewo ...

  8. Android开发学习之路-Handler消息派发机制源码分析

    注:这里只是说一下sendmessage的一个过程,post就类似的 如果我们需要发送消息,会调用sendMessage方法 public final boolean sendMessage(Mess ...

  9. Spring学习记录(四)---bean之间的关系:继承、依赖

         继承 这里说的继承和java的继承是不一样的,不是父类子类.但思想很相似,是父bean和子bean 1.父bean是一个实例时.它本身是一个完整的bean 2.父bean是模板,抽象bean ...

  10. c++头文件 #include<iostream>

    cout<<"C1="<<setiosflags(ios::fixed)<<setprecision(2)<<3.14*r*2< ...