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