1.类目(category)
类目的命名规则:类名+扩展方法,如“NSString+Revert”.
类目的接口声明与类的定义十分相似,但类目不继承父类,只需要带有一个括号,表明该类目的主要用途。
#import <Foundation/Foundation.h>
@interface NSString (Revert)
-(void)test;
@end
#import "NSString+Revert.h"
@implementation NSString (Revert)
-(void)test{
NSLog(@"这是类目的测试");
}
@end
2.延展(Extension)
类的延展就如同是 “匿名” 的类目,延展中声明的方法在类本身的
@implementation Person 和 它对应的 @end之间实现
#import "Person.h"
// 方法1 新建一个头文件,然后导入进来,再实现
/*
#import "Person_test.h"
@implementation Person
-(void)privateFunction{
NSLog(@"延展测试");
}
*/
@interface Person ()
-(void) privateFunction;
@end
@implementation Person
-(void)privateFunction{
NSLog(@"延展测试");
}
@end
3.协议 ( protocol)
协议的声明看起来比较类似一个类的接口,不同的是协议没有父类也不能定义实例变量。协议是一种特殊的程序设计结构,用于声明专门被别的类实现的方法。协议在以下场合非常有用:
①需要由别的类实现的方法
②声明未知类的接口
③两个类之间的通信
协议的基本特点
①协议可以被任何类实现的方法
②协议本身不是类,它是定义了一个其他类可实现的接口
③类目也可以采用协议
协议中的关键字
①@required:表示必须强制实现的方法
②@optional:表示可以有选择性的实现方法
下面的例子可以使用协议来进行传值
#import <UIKit/UIKit.h>
#import "DelegateDataViewController.h"
@interface DelegateViewController : UIViewController<DelegateDataViewControllerDelegate>
@end
#import "DelegateViewController.h"
@interface DelegateViewController ()
@property(nonatomic,retain) UITextField *textField;
@end
@implementation DelegateViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor =[UIColor scrollViewTexturedBackgroundColor];
_textField=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
_textField.backgroundColor=[UIColor whiteColor];
_textField.textAlignment=NSTextAlignmentCenter;
[self.view addSubview:_textField];
UIButton *getDataButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[getDataButton setFrame:CGRectMake(100, 80, 80, 80)];
[getDataButton setTitle:@"获取数据" forState:UIControlStateNormal];
[getDataButton addTarget:self action:@selector(getData) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:getDataButton];
UIButton *disButton=[UIButton buttonWithType:UIButtonTypeSystem];
[disButton setFrame:CGRectMake(100, 150, 80, 32)];
[disButton setTitle:@"返回主菜单" forState:UIControlStateNormal];
[disButton addTarget:self action:@selector(disAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:disButton];
}
-(void) getData
{
DelegateDataViewController *deleVC=[[DelegateDataViewController alloc]init];
deleVC.delegate=self;
[self presentViewController:deleVC animated:YES completion:nil];
}
-(void) disAction
{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void) sendText:(NSString *)text
{
self.textField.text=text;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
#import <UIKit/UIKit.h>
@protocol DelegateDataViewControllerDelegate <NSObject>
-(void)sendText:(NSString *)text;
@end
@interface DelegateDataViewController : UIViewController
@property (assign,nonatomic) id<DelegateDataViewControllerDelegate> delegate;
@end
#import "DelegateDataViewController.h"
@interface DelegateDataViewController ()
@end
@implementation DelegateDataViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self setUIView];
}
-(void) setUIView
{
self.view.backgroundColor=[UIColor groupTableViewBackgroundColor];
UIButton *button1=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setFrame:CGRectMake(100, 100, 100, 50)];
[button1 setTitle:@"1" forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(dataButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
UIButton *button2=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setFrame:CGRectMake(100, 200, 100, 50)];
[button2 setTitle:@"2" forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(dataButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
}
-(void) dataButtonAction:(UIButton *)button
{
if ([self.delegate respondsToSelector:@selector(sendText:)]) {
[self.delegate sendText:button.titleLabel.text];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
定时器(NSTimer) 的基本概念
一旦创建了一个定时器对象(NSTimer实例),它可以按照一定时间的间隔,将指定消息发送到目标对象,并更新某个对象的行为,你可以选择在未来的某个时候将它“开启”,或者将它停止乃至销毁。
NSRunloop基本概念
一个runloop就是一个事件处理的循环,用来不停的调度工作以及处理输入事件。使用run loop 的目的是让你的线程在有工作的时候忙于工作,而没工作的时候处于休眠状态。
在我们的应用程序中,你不需要创建NSRunloop对象。因为,在我们的主线程中(包括其他子线程) 系统会自动创建NSRunloop对象。如果你需要访问当前线程中的runloop,你可以通过类方法 "currentRunloop"