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私房菜》获奖公布

    <鸟哥的Linux私房菜>一书的赠书活动时间为2016年10月19日到10月31日, 也就是今天结束. 首先要感谢QQ号为:1084830483(路在远方),来自哈尔滨工程大学的同学赠送给 ...

  2. 译文---C#堆VS栈(Part One)

    前言 本文主要是讲解C#语言在内存中堆.栈的使用情况,使读者能更好的理解值类型.引用类型以及线程栈.托管堆. 首先感谢原文作者:Matthew Cochran 为我们带来了一篇非常好的文章,并配以大量 ...

  3. Opengl中矩阵和perspective/ortho的相互转换

    Opengl中矩阵和perspective/ortho的相互转换 定义矩阵 Opengl变换需要用四维矩阵.我们来定义这样的矩阵. +BIT祝威+悄悄在此留下版了个权的信息说: 四维向量 首先,我们定 ...

  4. [备忘]Redis运行出现Client sent AUTH, but no password is set

    原因:程序提供了密码,但是redis.conf中并没有设置密码. 附加问题:如果redis.conf中设置了密码,有可能会导致服务无法启动,报5013错误.可能是访问权限的问题.

  5. UWP自动填充控件AutoSuggestBox小优化

    UWP提供的AutoSuggestBox本身非常好用,在项目中经常用到,但是当我们使用时发现一下不人性化的设置,例子1如下: <Page x:Class="SelfInkCanvas. ...

  6. PHP制作查询租房表

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. 初学者--bootstrap(三)下载与安装----在路上(7)

    ----------------------------------------对于学习与工作者来说用压缩版,若是研究框架则用源代码---------------------------------- ...

  8. c#设计模式-组合模式

    在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系统中目录的处理就是这样的一个例子,因为目录可以包括单独的文件,也可以包括文件夹,文件夹又是由文件组成的,由于简单对象和复合对象 ...

  9. LINQ系列:Linq to Object串联操作符

    串联是一个将两个对象联接在一起的过程.在LINQ中,串联操作将两个集合合并成一个集合,通过Concat操作符实现. Concat 1>. 原型定义 public static IQueryabl ...

  10. WPF gridview 不允许编辑

    WPF gridview正常双击是运行编辑的,如何让他不允许编辑呢,如果采用readonly属性,在双击时会报错,当然可以通过try catch处理,但是这样不好,好一点的解决办法就是在绑定数据时采用 ...