Men的博客

欢迎光临!

0%

如何是用导航控制器显示第一个界面

创建导航控制器,把试图控制器放入导航控制器进行管理
UINavigationController *nc=[[UINavigationController alloc]initWithRootViewController:fvc];
试图控制器设置为导航控制器的根试图控制器
self.window.rootViewController=nc;

切换到下一个界面

先创建下一个界面
使用pushViewController切换过去
[self.navigationController pushViewController:svc animated:YES];
3.返回上一个界面
使用 popToViewController 返回过去
[self.navigationController popToViewController:svc Animated:YES];
4.从任意界面,返回根试图控制器
使用popToRootViewControllerAnimated
[self.navigationController popToRootViewControllerAnimated:YES];
5.从当前界面返回以前push的任意界面
使用popToViewController:array[1]
将每个试图放入数组中
NSArray *array=self.navigationController.viewControllers;
[self.navigationController popToViewController:array[1] animated:YES];

设置导航控制器的标题

fvc.title=@”第一界面”;

在导航栏上添加一个按钮(不是添加UIButton)

创建文本按钮initWithTitle
UIBarButtonItem *leftItem=[[UIBarButtonItem alloc]initWithTitle:@”分类” style:UIBarButtonItemStylePlain target:self action:@selector(changeCategory:)];
创建系统样式按钮initWithBarButtonSystemItem
UIBarButtonItem *leftItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(changeCategory:)];
创建自定义图片按钮initWithCustomView
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(0, 0, 35, 30);
[button setBackgroundImage:[UIImage imageNamed:@”main_left_nav”] forState:UIControlStateNormal];
UIBarButtonItem *leftItem=[[UIBarButtonItem alloc]initWithCustomView:button];
self.navigationItem.leftBarButtonItem=leftItem;

设置界面左上角返回按钮。

UIBarButtonItem *backItem=[[UIBarButtonItem alloc]initWithTitle:@”返回” style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.backBarButtonItem=backItem;

设置导航栏的背景

设置导航条的色调barTintColor
self.navigationController.navigationBar.barTintColor=[UIColor grayColor];

设置导航条的背景图片setBackgroundImage

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@”header_bg”] forBarMetrics:UIBarMetricsDefault];
UIBarMetricsDefault,
UIBarMetricsLandscapePhone,
UIBarMetricsDefaultPrompt = 101,
注意:导航条的大小:默认高度44个点。
导航条的图片高度要么是44,只会覆盖导航条
要么是64,不仅覆盖导航条,还会覆盖状态栏

隐藏导航栏

self.navigationController.navigationBar.hidden=YES;

工具栏的使用

添加工具栏(不是添加,是显示)
[self.navigationController setToolbarHidden:NO];
self.navigationController.toolbarHidden=NO;
工具栏上添加的控件只能是UIBarButtonItem
UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(dealTooBarClick:)];
UIBarButtonItem *item1=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(dealTooBarClick:)];
UIBarButtonItem *item2=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(dealTooBarClick:)];
self.toolbarItems=@[item,item1,item2];
为了分割三个Item,创建特殊item
相当于弹簧,弹开每个控件。UIBarButtonSystemItemFlexibleSpace
UIBarButtonItem *spse=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
可以自己设置宽度的间隔。UIBarButtonSystemItemFixedSpace
UIBarButtonItem *flex=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
flex.width=50;
self.toolbarItems=@[item,flex,item1,flex,item2];

注意:自动布局。self.view就是导航条以下的范围 。
self.automaticallyAdjustsScrollViewInsets=YES;
导航控制器:UINavigationController
导航条:navigationBar
导航条上的按钮控件:UIBarButtonItem
工具栏:Toolbar
注意:工具栏上的控件只能使用UIBarButtonItem设置

正向传值

1.什么是正向传值?
总结:A界面创建B界面
A界面的值传递到B界面
以后很多是有用到正向传值,只要在下一个界面的@proprty中添加一个属性。
实例:
登陆界面login 主界面main
把登陆界面上的用户名传到主界面
1.main上添加@proprty username
为什么定义@property属性
为了接受从登陆界面传过来的用户名。
@property (nonatomic ,copy)NSString *username;
2.传值。
MainViewController *mvc=[[MainViewController alloc]init];
把当前界面输入框中用户名传递给主界面。
注意:切换界面之前传值,千万不要放在present的后面。
mvc.username=_textField.text;
[self presentViewController:mvc animated:YES completion:nil];
3.第二个界面使用 self.object 使用值
nameLabel.text = [NSString stringWithFormat:@”用户名是: %@”,self.username];

反向传值

简单分解为四部分
1.制定协议
2.给主界面发送消息
3.主界面执行消息
4.配置界面的
1。先制定协议
为什么要制定协议
制定协议,协议中规定了配置界面应该给主界面发送什么消息
问题:谁制定协议呢?谁遵守协议呢?
配置界面制定协议,主界面遵守协议,实现协议中的方法
2.如何在配置界面添加主界面的指针
因为配置界面没有主界面的指针,所以,要把主界面的指针传到配置界面
给配置界面添加属性——传递主界面的指针
但是这个传入的对象必须要遵守协议,
代理一般使用assgin
3.如何传值
发送消息给配置界面让主界面去做事情
self,delegate代表主界面,self是配置界面。、

总结:
A界面创建B界面,
B把值(字体大小)传回给A界面
注意:使用到 协议,代理,比较复杂。
搞清楚问题。
主界面创建配置界面。
配置界面要修改主界面
但是配置界面没有主界面的指针。
MainViewController
ConfigViewController

(1)制定协议
因为是ConfigViewController其他界面发送消息

  1. 告诉其他界面我要给你发送什么消息
  2. 确保其他实现了消息对应的方法

@protocol ConfigViewControllerDelegate
//遵守协议的其他类必须实现的方法
-(void)changeFontSize:(int)size;
@end

@interface ConfigViewController : UIViewController

//作用: 保存主界面的指针
//为什么保存, 最后给主界面发送消息修改字体
//细节1: assgin 确保不会循环引用
//细节2: id 如果是id, 能传入任意界面了
//细节3: 遵守协议意味着可以任意对象, 但是这个对象必须遵守协议
@property (assign,nonatomic) id delegate;

@end

//int size = fontTextField.text.intValue
[self.delegate changeFontSize:fontTextField.text.intValue];
//切换到配置界面
ConfigViewController *cvc = [[ConfigViewController alloc] init];

//作用: 把当前界面的指针传到cvc中
//为啥: 希望配置界面中字体改变通知主界面
cvc.delegate = self;

[self presentViewController:cvc animated:YES completion:nil];

@interface MainViewController ()

//改变字体的方法
-(void)changeFontSize:(int)size
{
novelLabel.font = [UIFont systemFontOfSize:size];
}

单例传值

需求: A–>B–>C–>D–>E–>F
需要A中得数据在F界面上显示出来
解决: 使用单例传值解决
如何实现一个单例类?
实例: 在AppDelegate中存储作者和版本
在配置界面上显示出来
@interface DataCenter : NSObject
//表示获取一个共享的实例
+(id)sharedInstance;
@property (copy,nonatomic) NSString appInfo;
@end
@implementation DataCenter
表示获取一个共享的实例
+(id)sharedInstance
{
//关键
//第一次执行: dc为空, 会申请一个对象
//以后执行: dc不为空, 直接返回以前创建的对象
static DataCenter
dc= nil;
if(dc == nil)
{
//dc = [[DataCenter alloc] init];
dc = [[[self class] alloc] init];
}
return dc;
}
设置单利对象
DataCenter *dc=[DataCenter sharedInstance];
dc.author=@”quiet”;
先获取单利对象
DataCenter *dc=[DataCenter sharedInstance];
UILabel *authorLibel=[[UILabel alloc]initWithFrame:CGRectMake(100, 50, 100, 30)];
authorLibel.text=dc.author;
要求非常高单例类往往重写
alloc,dealloc,retain,release,
autorelease,copyWithZone

通知传值

需求: 1对n通信, 配置要换肤, 其他所有界面响应
发出换肤的通知
获取系统通知类的单例对象
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
发出一个通知
参数Name: 通知的名字
参数object: 要发给那个对象, nil表示不限制对象
参数userInfo: 通知附加的信息, 皮肤颜色穿过去
[nc postNotificationName:@”changeSkin” object:nil userInfo:@{@”skinColor”:@”yellow”}];
为了获取换肤的通知
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(dealChangeSkin:) name:@”changeSkin” object:nil];
}
注意: 参数类型不是NSNotificationCenter
-(void)dealChangeSkin:(NSNotification *)notification
{
获取通知中附加的信息
NSString *skinColor = notification.userInfo[@”skinColor”];
NSLog(@”c = %@”,skinColor);
if([skinColor isEqualToString:@”yellow”])
{
self.view.backgroundColor = [UIColor yellowColor];
}
}

block传值

实例化一个文本输入框

UITextField *file=[[UITextField alloc]init];

设置坐标

file.frame=CGRectMake(10, 30, 300, 30);

设置外观样式borderStyle

细节:输入框默认是没有边框的一定要设置边框
file.borderStyle=UITextBorderStyleBezel;
注意:文本框默认的颜色就是windiw的颜色,会跟着底色改变而改变
UITextBorderStyleNone, 没有样式
UITextBorderStyleLine, 一条黑色线框内部透明
UITextBorderStyleBezel, 一条棕色线框内部透明
UITextBorderStyleRoundedRect 圆角矩形:注意,内部默认为白色

设置背景颜色

file.backgroundColor=[UIColor blueColor];

设置提示文字placeholder:等待的地方

file.placeholder=@”请输入密码”;

设置密文输入secure牢固的 entry 进入输入

file.secureTextEntry=YES;

设置键盘样式

file.keyboardType=UIKeyboardTypeNumberPad;

设置键盘风格keyboardAppearance appearance:出现

file.keyboardAppearance=UIKeyboardAppearanceDefault;
UIKeyboardAppearanceAlert
UIKeyboardAppearanceDefault 系统默认
UIKeyboardAppearanceDark 黑色的
UIKeyboardAppearanceLight 亮色的

设置自定义弹出试图

UIImageView *imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@”2_1.jpg”]];
这里100的设置是无效的
imageView.frame=CGRectMake(0, 100, 320, 100);
弹出图片
file.inputView=imageView;

设置左,右视图(添加View或者ImageView)

在输入框左侧设置头像
UIImageView *hadeimage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 25, 30)];
hadeimage.image=[UIImage imageNamed:@”zhaoren”];
textField.leftView=hadeimage;
细节设置模式
textField.leftViewMode=UITextFieldViewModeAlways;

设置清除按钮模式clearButtonMode

file.clearButtonMode=UITextFieldViewModeAlways;
UITextFieldViewModeNever, 从不
UITextFieldViewModeWhileEditing, 当编辑的时候
UITextFieldViewModeUnlessEditing,当不编辑的时候
UITextFieldViewModeAlways 总是

再次编辑时是否清空clearsOnBeginEditing

file.clearsOnBeginEditing=YES;
这里为了区分,我们又设置了一个文本输入框textField
UITextField *textFlie=[[UITextField alloc]initWithFrame:CGRectMake(10, 100, 300, 100)];
textFlie.borderStyle=UITextBorderStyleRoundedRect;
[self.window addSubview:textFlie];
textFlie.font=[UIFont systemFontOfSize:24];

内容横向对其编辑方式contentHorizontalAlignment Horizontal水平的横向的

textFlie.contentHorizontalAlignment=
UIControlContentHorizontalAlignmentCenter;

内容纵向对其编辑模式 contentVerticalAlignment Vertical纵向的

textFlie.contentVerticalAlignment=UIControlContentHorizontalAlignmentCenter;
UIControlContentHorizontalAlignmentCenter = 0,中间
UIControlContentHorizontalAlignmentLeft = 1,左对齐
UIControlContentHorizontalAlignmentRight = 2,右对齐
UIControlContentHorizontalAlignmentFill = 3,充满的

文字内容对其方式textAlignment(注意:这里是NS)

textFlie.textAlignment=NSTextAlignmentCenter;
NSTextAlignmentCenter中间对齐
NSTextAlignmentRight靠右对齐
NSTextAlignmentLeft靠左对齐

设置滚动adjustsFontSizeToFitWidth

一开始开始缩小(注意:当文字小到一定地步的时候会不再缩小)
textFlie.adjustsFontSizeToFitWidth=YES;

设置最小字号(跟设置滚动有关)minimumFontSize

当滚动字号缩小到18的是候不在发生缩小
textFlie.minimumFontSize=18;

设置首字母是否大小写autocapitalizationType

textFlie.autocapitalizationType=UITextAutocapitalizationTypeSentences;
UITextAutocapitalizationTypeNone, 从不大写
UITextAutocapitalizationTypeWords, 单词首字母大写(以空格为区分)
UITextAutocapitalizationTypeSentences, 回车时第一个字符大写
UITextAutocapitalizationTypeAllCharacters,回车时当前的
关闭自动大写
textField.autocapitalizationType=UITextAutocapitalizationTypeNone;

关闭错误纠正(会记录之前输入的内容)autocorrectionType

textField.autocorrectionType=UITextAutocorrectionTypeNo;

设置return键样式returnKeyType

textFlie.returnKeyType=UIReturnKeyGo;
UIReturnKeyDefault,
UIReturnKeyGo,
UIReturnKeyGoogle,
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
()圆括号代表类扩展(匿名类别)
相当于.h中的@interface
只不过这里声明的方法供内部使用

设置输入框的背景图background

细节:添加背景图要吧边框类型设置为无边框
textField.borderStyle=UITextBorderStyleNone;
textField.background=[[UIImage imageNamed:@”table-cell-bg-highlighted”]stretchableImageWithLeftCapWidth:8 topCapHeight:0];
图片拉伸中的问题。图片是向左向上拉伸的。
拉伸的距离相当于图片最顶端和最左边向里多少距离的位置向上向左拉伸

简单的理解一个界面就是一个UIViewContorller

如何使用UIViewContorller实现多界面的切换

1.登陆界面:
先创建LoginViewController对象
将对象设置为window的rootViewController视图控制器 (主视图控制器)
2.跳转到下一个界面(设置按钮触发点击事件)
先创建下一个界面 MainViewController.的对象———mvc
使用presentViewController切换到下一个界面
参数1:跳转到界面mcv
参数2:是否实现动画效果。
参数3:block
[self presentViewController:mvc animated:YES completion:nil];
3.返回上一个界面
同样是设置按钮,点击按钮触发事件。
释放掉原来的界面,显示出上一个界面。dismissViewControllerAnimated
[self dismissViewControllerAnimated:YES completion:nil];
===================================================================
4.设置打开动画
mvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
翻转 UIModalTransitionStyleFlipHorizontal
交错 UIModalTransitionStyleCrossDissolve
翻页 UIModalTransitionStylePartialCurl

创建

UIView *view=[[UIView alloc]init];

坐标

view.frame=CGRectMake(100, 100, 100, 100);

中心位置

view.center=CGPointMake(200, 200);

大小 不想改变位置,想改变大小(设置bounds)

view.bounds=CGRectMake(0, 0, 50, 50);

tag值 区分不同控件(区分多个按钮)

view.tag=100;

是否打开用户交互

有的控件UIImageView默认不支持
后果:按钮直接加到UIImageView无法点击
view.userInteractionEnabled=YES;

视图之间的相互组合嵌套

[view addSubview:view1]

视图的层次感(层级关系)

视图层次感规律:先加入的在下面,后加入的在上面。

  1. 获取window的所有子视图
    NSLog(@”%@”,self.window.subviews);

UIView *view1=[[UIView alloc]initWithFrame:CGRectMake(100, 240, 100, 100)];
view1.backgroundColor=[UIColor redColor];
[self.window addSubview:view1];
UIView *view2=[[UIView alloc]initWithFrame:CGRectMake(150, 310, 100, 100)];
view2.backgroundColor=[UIColor greenColor];
[self.window addSubview:view2];
UIView *view3=[[UIView alloc]initWithFrame:CGRectMake(200, 360, 100, 100)];
view3.backgroundColor=[UIColor blueColor];
[self.window addSubview:view3];
2.把view2放到最上面
[self.window bringSubviewToFront:view2];
3.把view2放到最下面
[self.window sendSubviewToBack:view2];
4.把某个视图插入到某个视图上面
UIView *view4=[[UIView alloc]initWithFrame:CGRectMake(120, 260, 100, 100)];
view4.backgroundColor=[UIColor purpleColor];
把视图插入到某个视图上面
[self.window insertSubview:view4 belowSubview:view1];
5.交换两个层
[self.window exchangeSubviewAtIndex:1 withSubviewAtIndex:2];
6.图层是可以放进数组里面
NSArray *subViews=self.window.subviews;
注意:当你交换了两个图层位置时,数组中相应的位置也会发生改变。
7.隐藏一个视图
view4.hidden=YES;
8.删除一个视图。
[view4 removeFromSuperview];

声明

UIImageView *imageView = [[UIImageView alloc] init];
iphone5s 320x568
细节: 显示图片根据frame缩放

设置位置

imageView.frame = CGRectMake(0, 0, 320, 568);

添加图片

(注意:image是UIImageView的必备属性 )
imageView.image = [UIImage imageNamed:@”back2.jpg”];

显示到桌面

[self.window addSubview:imageView];

在设置背景图片时的拉伸问题

图片拉伸中的问题。图片是向左向上拉伸的。
拉伸的距离相当于图片最顶端和最左边向里多少距离的位置向上向左拉伸
UIImageView *im=[[UIImageView alloc]initWithFrame:CGRectMake(100, 200, 100, 100)];
im.image=[[UIImage imageNamed:@”logo_title”]stretchableImageWithLeftCapWidth:4 topCapHeight:4];

按钮的创建

以后经常使用类方法创建按钮buttonWithType
UIButton *button = [[UIButton alloc]init];
系统样式的按钮 UIButtonTypeSystem
自定义的按钮 UIButtonTypeCustom
详情按钮 UIButtonTypeDetailDisclosure,
灰色按钮 UIButtonTypeInfoLight,
白色信息按钮 UIButtonTypeInfoDark,
加号按钮 UIButtonTypeContactAdd,
圆角按钮 UIButtonTypeRoundedRect
ios6系统按钮是圆角矩形
ios7取消了边框

坐标

button.frame= CGRectMake(100, 100, 100, 30);

设置文本

注意:按钮有多种状态,(正常,高亮,禁止)
UIControlStateNormal表示正常状态
UIControlStateHighlighted表示高亮状态
[button setTitle:@”按我啊” forState:UIControlStateNormal];
3.1文本颜色
设置文本颜色为红色。
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
3.2设置文本字体
button.titleLabel.font=[UIFont systemFontOfSize:17];

按钮的tag值

button.tag=100;

点击时效果

点击时高亮button.showsTouchWhenHighlighted=YES;
禁用状态下按钮是否变暗adjustsImageWhenDisabled
高亮状态下按钮是否变暗adjustsImageWhenHighlighted

按钮的点击事件处理

告诉按谬你被执行我定义方法
UIControlEventTouchUpInside按钮被点击后弹起,
参数1,参数2,哪个对象哪个方法,
当按钮被点击弹起瞬间执行方法UIControlEventTouchUpInside
[button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

启用禁用按钮

button.enabled=YES;

设置圆角矩形

button.layer.cornerRadius=5;
允许剪切,不加可能没有效果。
button.clipsToBounds=YES;

图片按钮

1.图片按钮要设置为自定义类型的按钮。
UIImage *image=[UIImage imageNamed:@”屏幕快照 2015-01-21 下午2.54.00.png”];
添加图片
[imageButton setBackgroundImage:image forState:UIControlStateNormal];
2.设置图片文字的位置(可以实现 图片文字在按钮上上下左右移动)
imageButton.imageEdgeInsets=UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right);
imageButton.titleEdgeInsets = UIEdgeInsetsMake(0, -100, 0, 0)
注意 :setImage 和setBackgroundImage区别
setImage是设置图片,不可以改变图片的大小,可以通过imageEdgeInsetsMake设置图片的位置,
setBackgroundImage是将图片设置为按钮的背景,相当于平铺效果。

在按钮上添加UIImageView

1.声明
UIImageView *imageView = [[UIImageView alloc] init];
2.设置位置(注意以父视图为准)
imageView.frame = CGRectMake(0, 0, 150, 30);
3.添加图片
imageView.image = [UIImage imageNamed:@”back2.jpg”];
4.添加到按钮上
[imagebutton addSubview:imageView];

创建UIlabel

UILabel *label=[[UILabel alloc]init];

坐标

label.frame=CGRectMake(100, 100, 200, 30);

文本

label.text=@”我是标签~”;

颜色

1.文字颜色
label.textColor=[UIColor orangeColor];
2.三原色设置颜色。
label.textColor = [UIColor colorWithRed:0.6 green:0.3 blue:0.5 alpha:1];
3.设置高亮文字颜色
highlightedTextColor
4.标签背景颜色
label.backgroundColor=[UIColor orangeColor];

字体lable.font

label.font = [UIFont systemFontOfSize:17];
粗体加设置大小。
label.font = [UIFont boldSystemFontOfSize:20];
自定义字体
label.font = [UIFont fontWithName:@”Arial” size:17];
根据label宽度设置字体大小
adjustFontSizeToFitWidth
显示所有字体
NSLog(@”所有字体%@”,[UIFont familyNames]);

对其方式

设置标签的背景颜色backgroundColor
label.backgroundColor=[UIColor grayColor];
靠右对其,默认靠左对其textAlignment
label.textAlignment = NSTextAlignmentRight;
如何显示多行文本
0表示任意多行。labe.numberOfLines
label.numberOfLines = 0;

设置阴影

设置阴影颜色shadowColor
label.shadowColor = [UIColor redColor];
设置阴影偏移shadowOffset
label.shadowOffset = CGSizeMake(3, 3);

设置窗口显示的文本sizeToFit

自动适应文本作用:文本有多大,标签就有多大
[label sizeToFit];
最大显示行数
numberOflines
当内容超出宽度模式
lineBreakMode

继承

什么是继承,继承如何实现?

总结: 继承的语法 @interface Student : Person
作用- 子类拥有父类的属性和方法
语法: 定义了Student, 继承与Person
Student子类, Person父类
学生信息管理系统
人 Person人 属性: 名字,性别,年龄
学生 Student学生 属性: 名字,性别,年龄, 学号, OC成绩

继承后的重写

子类拥有父类所有的方法
如果子类中重写了从父类继承下来的方法
优先执行重写后的方法

继承后的权限

总结: 子类能访问父类公有和受保护的属性和方法
不能访问私有的, 想要访问, 使用父类
getter和setter间接访问
只写@property,没有写对应的实例变量
权限是私有的private

父类指针和子类指针如何相互指向?

(赋值兼容原则)
总结: 父类指针可以指向父类对象, 还有子类对象
子类指针只能指向它自己的对象, 不能指向父类对象
NSString —-> NSMutableString
NSArray —–> NSMutableArray

init方法 super和self, NSObject

NSObject是oc中所有类的根类
提供了重要的方法alloc init
self 在方法中表示当前对象,类方法中表示当前类
super 一般用super执行父类的方法

isKindClassOf和isMemberClassOf

判断一个对象是不是某个类的实例?
总结:
isKindOfClass 判断一个对象是否是这个类及其子类的实例
isMemberOfClass 判断一个对象是不是某个类的实例

类别Category

什么是类别?, 类别的作用

需求: 有些类, 缺少我们需要一些方法
但是我没有类的源代码, 添加一个方法
实例: NSMutableString 添加一个reverse方法
类别是OC语法, 能给系统或自定义的类添加一个方法

类别基本使用(语法)

使用类别, 需要包含类别头文件
#import “NSMutableString+Extension.h”
@interface NSMutableString (Extension)
需要添加的方法的声明
-(void)reverseString
{
//逆序当前字符串 self
NSMutableString *mstr = [[NSMutableString alloc] init];
for (long i=self.length-1; i>=0; i–) {
[mstr appendFormat:@”%C”,[self characterAtIndex:i]];
}
self.string = mstr;
}
[self setString mstr]
self.string=mstr;
// NSLog(@”%p”,self);
// 当前对象指针。

类别的限制(使用注意事项)

(1) 系统中已经有function方法, 添加类别,类别中也有function方法
总结: 类别中得方法如果在类中已经存在
优先使用类别中得方法
(2) 类别能给一个类添加方法? 能不能添加实例变量?
总结: 类别中不能添加实例变量

类别和继承有什么区别? 在什么情况下使用类别,或继承

总结: 一般情况下
有一个类, 缺少一些方法, 扩展这个类, 使用类别添加方法
@intereface Dog (Ext)
有一个类, 缺少方法, 缺少属性, 使用继承, 在子类中添加方法和属性
@intereface Dog : NSObject