Gowhich

Durban's Blog

如何在iOS设备上安装调试多个App

这个话题很重要,经过我的不断关注,在一个高手博客里面找到了,简单的贴到我这里了哈

作为iOS开发工程师, 在发布app之前,你需要在真机上调试、测试,所以需要将app 下载到真机上。 如果想同时下载多个App ,怎么办呢? 我们先来看看App 的工作原理。

Appstore上的每个app 都有一个唯一的ID。 这个ID,如同人们的身份证一样,每个App 都不是不同的。 但作为iOS开发者来说,在App 调试阶段,你可以自行设置多个App ID。 因为这些App 还没有发布到Appstore 上,你只需要保证自己开发的App 设置不同的App ID 即可。

只要App ID 不同, 你就可以在同一部iOS设备上,安装不同的App。

具体到开发层面, 你需要在xcode 的 info.plist 文件的 Bunlde Identifier 设置 App ID。

举例来说:

如果你的 mobileprovisioning Proifle (dev_any_profile)文件对应的 Bunld ID 为: com.leopard.* 这个 * 就是一个通配符。 你可以用不同的字符替换。

在 info.plist 文件的 Bunlde Identifier 中,填写 com.leopard.app1 , 在 project -> build setting -> code siging 中,将对应的 dev_any_profile 关联起来, 这样就生成了一个 app id 为 app1 的 App;

同理,在 info.plist 文件的 Bunlde Identifier 中,填写 com.leopard.app2, 在 project -> build setting -> code siging 中,将对应的 dev_any_profile 关联起来, 这样就生成了一个 app id 为 app2 的 App;

以此类推, 便可以生成多个App。 因为它们的 app id 不同, 便可以同时安装在同一部iOS设备上。

“如何安装 ad-hoc ipa ”,说到这个话题,也是我最近很关注的,因为方便老板们或者其他人的测试,需要这个文件,具体怎么回事,我记到下面了,这篇文章也是转载一个高手的,专注移动端的

如果你是 iOS 开发者, 给客户开发的app, 在发布到appstore 前,需经过客户的测试。 

如果客户的iOS设备不是越狱的,你只好通过 ad-hoc 模式,将生产的 ad-hoc profile 和 ipa 文件发给客户。  (如何生成这两个文件,不在此赘述)。  

客户如何安装这两个文件,这是我一直头疼的。 我们无法要求客户有技术背景。 相反,大多客户为非技术人员。 客户是上帝,绝不能苛求客户。
为此,我写过多个版本的文档, 但操作步骤,连我自己都觉得麻烦。
早期,是基于 iTunes 同步安装。 但iTunes 的同步操作,太复杂了。 iTunes 版本每次更新,我都会被困惑一次。 iTunes 可谓功能强大,但对用户的使用技巧要求也很高。 如果客户仅仅是为了安装一个 ipa, 而被迫熟悉 iTunes。 其难度可想而知。
其实, iTunes同步ipa 还算简单,难点在于,如何告知客户安装 那个 ad-hoc provisioning profile。 通常是无果而终。
可以说,让客户通过 iTunes 安装 ad-hoc ipa,这个过程相当艰难。
这里推荐一种我所知道的最快捷的方法。

ad-hoc profile 文件的安装

将 ad-hoc provisioning profile 通过邮件发给客户, 客户一定要通过手机接收这个邮件 (不要通过PC接收邮件)。 客户从手机上打开邮箱, 点击 这个附件 (profile 文件), 直接安装到 iOS 设备上。 (只需点击这个附件,即可安装成功);
这时,你需要查看下ad-hoc profile是否安装成功。 方法如下:
打开iOS 设备的 设置-> 通用-> 描述文件,

ipa 文件的安装

建议客户下载 iTools。 我一直是推崇apple 的官方安装渠道(通过iTunes)的,但这一次推荐iTools 。 对安装一个pia 来说, iTools 的强大的地方在于,你只需要在 PC上 双击 ipa 文件, 这个ipa 会自动导入到 iTools 中,而且会给出一个赫然安装提示。这一点,对于非技术人员来说,至关重要。 前提是,你的iOS设备需通过数据线连接到PC上。 这么醒目的 “立即安装”提示, 很直观吧

不知道什么原因,自己在做删除操作的时候,会有个错误,总是删除了,又以空的形式出现,我火了。

查找了很多资料,自己拼出一个还算能够使用的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
-(void) refreshData
{
NSMutableDictionary *tmpMutableDictionary = [NSMutableDictionary dictionaryWithCapacity:20];
NSArray *allKeys = [self.dataDic allKeys];
NSMutableArray *allMutableKeys = [allKeys mutableCopy];
NSComparator cmptr = ^(id obj1, id obj2){
if ([obj1 integerValue] > [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}

if ([obj1 integerValue] < [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
};
NSArray *resultKeys = [allMutableKeys sortedArrayUsingComparator:cmptr];

int k = 0;
for (id item in resultKeys) {
NSDictionary *dic = [self.dataDic valueForKey:[NSString stringWithFormat:@"%@",item]];
[tmpMutableDictionary setValue:dic forKey:[NSString stringWithFormat: @"%d", k]];
k++;
}

self.dataDic = [tmpMutableDictionary mutableCopy];
}

第一种,利用数组的sortedArrayUsingComparator调用 NSComparator ,obj1和obj2指的数组中的对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
NSComparator cmptr = ^(id obj1, id obj2){
if ([obj1 integerValue] > [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}

if ([obj1 integerValue] < [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
};

NSArray *sortArray = [[NSArray alloc] initWithObjects:@"1",@"3",@"4",@"7",@"8",@"2",@"6",@"5",@"13",@"15",@"12",@"20",@"28",@"",nil];
//排序前
NSMutableString *outputBefore = [[NSMutableString alloc] init];
for(NSString *str in sortArray){
[outputBefore appendFormat:@""];
}
NSLog(@"排序前:%@",outputBefore);
//排序后
NSArray *array = [sortArray sortedArrayUsingComparator:cmptr];
NSMutableString *outputAfter = [[NSMutableString alloc] init];
for(NSString *str in array){
[outputAfter appendFormat:@"];
}
NSLog(@"排序后:%@",outputAfter);

第二种 排序方法 利用sortedArrayUsingFunction 调用 对应方法customSort,这个方法中的obj1和obj2分别是指数组中的对象。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
NSInteger customSort(id obj1, id obj2,void* context){
if ([obj1 integerValue] > [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}

if ([obj1 integerValue] < [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}

NSArray *sortArray = [[NSArray alloc] initWithObjects:@"1",@"3",@"4",@"7",@"8",@"2",@"6",@"5",@"13",@"15",@"12",@"20",@"28",@"",nil];
//排序前
NSMutableString *outputBefore = [[NSMutableString alloc] init];
for(NSString *str in sortArray){
[outputBefore appendFormat:@""];
}
NSLog(@"排序前:%@",outputBefore);

NSArray *array = [sortArray sortedArrayUsingFunction:customSort context:nil];

NSMutableString *outputAfter = [[NSMutableString alloc] init];
for(NSString *str in array){
[outputAfter appendFormat:@""];
}
NSLog(@"排序后:%@",outputAfter);

第三种 利用sortUsingDescriptors调用NSSortDescriptor

1
2
3
4
5
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"price" 
ascending:NO];//其中,price为数组中的对象的属性,这个针对数组中存放对象比较更简洁方便
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];
[_totalInfoArray sortUsingDescriptors:sortDescriptors];
[_airListView refreshTable:_totalInfoArray];

字符串的比较模式:

1
2
3
4
5
6
7
8
9
10
11
12
13
NSComparator cmptr = ^(id obj1, id obj2){
if([[NSString stringWithFormat:@"%@",obj1] compare:[NSString stringWithFormat:@"%@",obj2] options:NSNumericSearch] > 0)
{
return (NSComparisonResult)NSOrderedDescending;
}

if([[NSString stringWithFormat:@"%@",obj1] compare:[NSString stringWithFormat:@"%@",obj2] options:NSNumericSearch] < 0)
{
return (NSComparisonResult)NSOrderedAscending;
}

return (NSComparisonResult)NSOrderedSame;
};

数字比较模式:

1
2
3
4
5
6
7
8
9
10
NSInteger customSort(id obj1, id obj2,void* context){
if ([obj1 integerValue] > [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}

if ([obj1 integerValue] < [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}

最近想到了这些的东西,就去google搜索了一下,结果还真有,哈,知识在于发现,代码贴到下面了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
NSLog(@"uniqueIdentifier: %@", [[UIDevice currentDevice] uniqueIdentifier]);
NSLog(@"name: %@", [[UIDevice currentDevice] name]);
NSLog(@"systemName: %@", [[UIDevice currentDevice] systemName]);
NSLog(@"systemVersion: %@", [[UIDevice currentDevice] systemVersion]);
NSLog(@"model: %@", [[UIDevice currentDevice] model]);
NSLog(@"localizedModel: %@", [[UIDevice currentDevice] localizedModel]);

NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];

CFShow(infoDictionary);

// app名称
NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];

// app版本
NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];

// app build版本
NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];

UIBbarButtonItem的初始化。

根据SDK的文档,我们可以发现UIBbarButtonItem有如下几种初始化的方法:

  • -initWithTitle
  • -initWithImage
  • -initWithBarButtonSystemItem
  • -initWithCustomView

第4种方法就是我们添加各种作料的接口,所以今天的主角其它也是它。

在UIToolBar上面添加Title

1
2
3
4
5
6
UIToolbar *myToolBar = [[UIToolbar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
NSMutableArray *myToolBarItems = [NSMutableArray array];
[myToolBarItems addObject:[[[UIBarButtonItem alloc] initWithTitle:@"myTile" style:UIBarButtonItemStylePlain target:self action:@selector(action)] autorelease]];
[myToolBar setItems:myToolBarItems animated:YES];
[myToolBar release];
[myToolBarItems];

setItems传入值或者说items是一个对象数组。

在UIToolBar上面添加image

1
[myToolBarItems addObject:[[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"myImage.png"] style:UIBarButtonItemStylePlain target:self action:@selector(action)]];

在UIToolBar上面添加SystemItem

1
[myToolBarItems addObject:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(action)] autorelease]];

Note:

initWithBarButtonSystemItem初始化:

- (id)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(id)target action:(SEL)action

Defines system defaults for commonly used items.

typedef enum {
UIBarButtonSystemItemDone,
UIBarButtonSystemItemCancel,
UIBarButtonSystemItemEdit,
UIBarButtonSystemItemSave,
UIBarButtonSystemItemAdd,
UIBarButtonSystemItemFlexibleSpace,
UIBarButtonSystemItemFixedSpace,
UIBarButtonSystemItemCompose,
UIBarButtonSystemItemReply,
UIBarButtonSystemItemAction,
UIBarButtonSystemItemOrganize,
UIBarButtonSystemItemBookmarks,
UIBarButtonSystemItemSearch,
UIBarButtonSystemItemRefresh,
UIBarButtonSystemItemStop,
UIBarButtonSystemItemCamera,
UIBarButtonSystemItemTrash,
UIBarButtonSystemItemPlay,
UIBarButtonSystemItemPause,
UIBarButtonSystemItemRewind,
UIBarButtonSystemItemFastForward,
UIBarButtonSystemItemUndo, // iPhoneOS 3.0
UIBarButtonSystemItemRedo, // iPhoneOS 3.0
} UIBarButtonSystemItem;

在UIToolBar上面添加其它各种控件

最自由意义,最有意思的,我把它放在最后来讲。我们使用initWithCustomView来完成,

这里需要看一下initWithCustomView的定义:

1
- (id)initWithCustomView:(UIView *)customView

可以看出,它的参数是一个VIEW,所以我们给它的配料要正确哦才行哦,否则,你就等着时间DIDADIDA的流失吧.

A>加一个开关switch:

1
2
3
[myToolBarItems addObject:[[[UIBarButtonItem alloc]   
initWithCustomView:[[[UISwitch alloc] init] autorelease]]
autorelease]];

B>加一个按钮UIBarButtonItem

1
2
3
4
5
6
7
UIBarButtonItem *myButton = [[[UIBarButtonItem alloc]  
initWithTitle:@"myButton"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(action)]autorelease];
get1Button.width = 50;
[myToolBarItems addObject:myButton];

C>加一个文本Label

1
2
3
4
5
6
7
8
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(40.0f, 20.0f, 45.0f, 10.0f)];  
myLabel.font=[UIFont systemFontOfSize:10];
//myLabel.backgroundColor = [UIColor clearColor];
//myLabel.textAlignment=UITextAlignmentCenter;
UIBarButtonItem *myButtonItem = [[UIBarButtonItem alloc]initWithCustomView:myLabel];
[myToolBarItems addObject: myButtonItem];
[mylabel release];
[myButtonItem release];

D>加一个进度条UIProgressView

1
2
3
4
5
UIProgressView *myProgress = [[UIProgressView alloc] initWithFrame:CGRectMake(65.0f, 20.0f, 90.0f, 10.0f)];  
UIBarButtonItem *myButtonItem = [[UIBarButtonItem alloc]initWithCustomView:myProgress];
[myToolBarItems addObject: myButtonItem];
[myProgress release];
[myButtonItem release];

可以加使用initWithCustomView制作各种button,这里就不在这里一个一个在加了。我想你应该也已经掌握了如何添加各种buttonItem的方法了。

我是第一次使用这个东西,经过自己资料的查找,终于有了我觉得比较好用的方法

如下代码

1
2
3
4
5
6
7
8
UIButton *nextStep = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 10.0, 70.0, 30.0)];
[nextStep setImage:[UIImage imageNamed:@"button-下一步.png"] forState:UIControlStateNormal];
[nextStep addTarget:self action:@selector(doneAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *nextStepBarBtn = [[UIBarButtonItem alloc] initWithCustomView:nextStep];
//[nextStepBarBtn setWidth:1080];
UIBarButtonItem *spaceButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFixedSpace target:nil action:nil];
[spaceButtonItem setWidth:540];
[myToolBar setItems:[NSArray arrayWithObjects:spaceButtonItem,nextStepBarBtn,nil] animated:YES];

这里面用的是横屏的,我针对于自己的项目,需要的是竖屏的,结果的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
UIToolbar *attentionToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height - 100.0, self.view.frame.size.width, 60.0)];
attentionToolBar.tintColor = [UIColor redColor];
attentionToolBar.backgroundColor = [UIColor redColor];
[self.view addSubview:attentionToolBar];


UIButton *attentionBar = [[UIButton alloc] initWithFrame:CGRectMake(200.0, 10.0, 80.0, 30.0)];
[attentionBar setTitle:@"添加关注" forState:UIControlStateNormal];
[attentionBar addTarget:self action:@selector(addAttention:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *nextStepBarBtn = [[UIBarButtonItem alloc] initWithCustomView:attentionBar];

UIBarButtonItem *spaceButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFixedSpace target:nil action:nil];
[spaceButtonItem setWidth:(self.view.frame.size.width - attentionBar.frame.size.width)/2];

[attentionToolBar setItems:[NSArray arrayWithObjects:spaceButtonItem,nextStepBarBtn,nil] animated:YES];

关于UILabel的用法前面有篇文章已经说过,但是我看过后还有有些缺少,之后自己又找了很多,现在记录一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
- (void)viewDidLoad {

//创建uilabel

UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 280, 80)];

//设置背景色

label1.backgroundColor = [UIColor grayColor];

//设置tag

label1.tag = 91;

//设置标签文本

label1.text = @"CCBASE.NET!";

//设置标签文本字体和字体大小

label1.font = [UIFont fontWithName:@"Arial" size:30];

//设置文本对齐方式

label1.textAlignment = UITextAlignmentCenter;

//文本对齐方式有以下三种

//typedef enum {

// UITextAlignmentLeft = 0,左对齐

// UITextAlignmentCenter,居中对齐

// UITextAlignmentRight, 右对齐

//} UITextAlignment;

//文本颜色

label1.textColor = [UIColor blueColor];

//超出label边界文字的截取方式

label1.lineBreakMode = UILineBreakModeTailTruncation;

//截取方式有以下6种

//typedef enum {

// UILineBreakModeWordWrap = 0, 以空格为边界,保留整个单词

// UILineBreakModeCharacterWrap, 保留整个字符

// UILineBreakModeClip, 到边界为止

// UILineBreakModeHeadTruncation, 省略开始,以……代替

// UILineBreakModeTailTruncation, 省略结尾,以……代替

// UILineBreakModeMiddleTruncation,省略中间,以……代替,多行时作用于最后一行

//} UILineBreakMode;

//文本文字自适应大小

label1.adjustsFontSizeToFitWidth = YES;

//当adjustsFontSizeToFitWidth=YES时候,如果文本font要缩小时

//baselineAdjustment这个值控制文本的基线位置,只有文本行数为1是有效

label1.baselineAdjustment = UIBaselineAdjustmentAlignCenters;

//有三种方式

//typedef enum {

// UIBaselineAdjustmentAlignBaselines = 0, 默认值文本最上端于label中线对齐

// UIBaselineAdjustmentAlignCenters,//文本中线于label中线对齐

// UIBaselineAdjustmentNone,//文本最低端与label中线对齐

//} UIBaselineAdjustment;

//文本最多行数,为0时没有最大行数限制

label1.numberOfLines = 2;

//最小字体,行数为1时有效,默认为0.0

label1.minimumFontSize = 10.0;

//文本高亮

label1.highlighted = YES;

//文本是否可变

label1.enabled = YES;

//去掉label背景色

//label1.backgroundColor = [UIColor clearColor];

//文本阴影颜色

label1.shadowColor = [UIColor grayColor];

//阴影大小

label1.shadowOffset = CGSizeMake(1.0, 1.0);

//是否能与用户交互

label1.userInteractionEnabled = YES;

[self.view addSubview:label1];

[label1 release];

[super viewDidLoad];

}

关于显示和隐藏TabBar的方法,自己开始不是很懂,查找了很多的资料,在http://blog.csdn.net/riveram/article/details/7345577

这里面,有这样的描述,里面是给了两个方法:

隐藏tabbar的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
- (void)hideTabBar {
if (self.tabBarController.tabBar.hidden == YES) {
return;
}
UIView *contentView;
if ( [[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] )
contentView = [self.tabBarController.view.subviews objectAtIndex:1];
else
contentView = [self.tabBarController.view.subviews objectAtIndex:0];
contentView.frame = CGRectMake(contentView.bounds.origin.x, contentView.bounds.origin.y, contentView.bounds.size.width, contentView.bounds.size.height + self.tabBarController.tabBar.frame.size.height);
self.tabBarController.tabBar.hidden = YES;

}

显示tabbar的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- (void)showTabBar

{
if (self.tabBarController.tabBar.hidden == NO)
{
return;
}
UIView *contentView;
if ([[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]])

contentView = [self.tabBarController.view.subviews objectAtIndex:1];

else

contentView = [self.tabBarController.view.subviews objectAtIndex:0];
contentView.frame = CGRectMake(contentView.bounds.origin.x, contentView.bounds.origin.y, contentView.bounds.size.width, contentView.bounds.size.height - self.tabBarController.tabBar.frame.size.height);
self.tabBarController.tabBar.hidden = NO;

}

然后直接调用就好了。

另外在cocoachina的网站上,也看到了一个方法,不过只是说是隐藏的方法。

代码如下:

隐藏Tabbar

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
- (void)makeTabBarHidden:(BOOL)hide
{
if ( [self.tabBarController.view.subviews count] < 2 )
{
return;
}
UIView *contentView;

if ( [[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] )
{
contentView = [self.tabBarController.view.subviews objectAtIndex:1];
}
else
{
contentView = [self.tabBarController.view.subviews objectAtIndex:0];
}
// [UIView beginAnimations:@"TabbarHide" context:nil];
if ( hide )
{
contentView.frame = self.tabBarController.view.bounds;
}
else
{
contentView.frame = CGRectMake(self.tabBarController.view.bounds.origin.x,
self.tabBarController.view.bounds.origin.y,
self.tabBarController.view.bounds.size.width,
self.tabBarController.view.bounds.size.height - self.tabBarController.tabBar.frame.size.height);
}

self.tabBarController.tabBar.hidden = hide;
// [UIView commitAnimations];
}

但是,有经过资料的查找,有个比较简单的方法

我在A视图的代码可参考如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
-(void) viewWillAppear:(BOOL)animated
{


//添加右侧的item
self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
_rightButton.titleLabel.font = [UIFont fontWithName:@"Avenir-Book" size:14.0];
_rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
_rightButton.contentEdgeInsets = UIEdgeInsetsMake(0.0, 2.0, 0.0, 0.0);
[_rightButton setFrame:CGRectMake(0.0, 0.0, 60.0, 30.0)];
NSString *arrowPath = [[NSBundle mainBundle] pathForResource:@"littleArrow" ofType:@"png"];

[_rightButton setBackgroundImage:[UIImage imageWithContentsOfFile:arrowPath] forState:UIControlStateNormal];
[_rightButton addTarget:self
action:@selector(personRoleSelect:)
forControlEvents:UIControlEventTouchUpInside];

if([_role isEqualToString:@""] || (_role == nil))
{
[_rightButton setTitle:@"全部" forState:UIControlStateNormal];
}
else
{
[_rightButton setTitle:[NSString stringWithFormat:@"%@",_role] forState:UIControlStateNormal];
}



[_rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

UIBarButtonItem *rightItemBar = [[UIBarButtonItem alloc] initWithCustomView:_rightButton];
self.navigationItem.rightBarButtonItem = rightItemBar;

self.hidesBottomBarWhenPushed = YES;

UIBarButtonItem *returnItem = [[UIBarButtonItem alloc] init];
returnItem.title = @"返回";
self.navigationItem.backBarButtonItem = returnItem;
}

-(void) viewWillDisappear:(BOOL)animated
{
self.hidesBottomBarWhenPushed = NO;
[_dropDown hideDropDown:_rightButton];
}

主要是这句:

self.hidesBottomBarWhenPushed = YES;

self.hidesBottomBarWhenPushed = NO;

1.object-c最好 用nil [nil 任意方法],不会崩溃
nil 是一个对象值。
NULL 是一个通用指针(泛型指针)。

  1. NSNULL,NULL和nil在本质上应该是一样的,NULL和nil其实就是0,但是在Objective-c中,对于像NSArray这样的类型,nil或NULL不能做为加到其中的Object,如果定义了一个NSArray,为其分配了内存,又想设置其中的内容为空,则可以用[NSNULL null返回的对象来初始化NSArray中的内容,
    3.因为在NSArray和NSDictionary中nil中有特殊的含义(表示列表结束),所以不能在集合中放入nil值。如要确实需要存储一个表示“什么都没有”的值,可以使用NSNull类。NSNull只有一个方法:
  • + (NSNull *) null;

因为Object-C的集合对象,如NSArray、NSDictionary、NSSet等,都有可能包含NSNull对象,所以,如果一下代码中的item为NSNull,则会引起程序崩溃。

以下代码是常见的错误,release对象没有设置为nil,从而引起程序崩溃。

1
2
3
4
5
NSString *item=[NSArray objectAtIndex:i];
if([item isEqualToString:@"TestNumber"])
{
//
}
1
2
3
4
5
6
7
8
id someObject=[[Object alloc] init];
//...
[someObject release];
//...
if(someObject)
{
//crash here
}

nil用来给对象赋值(Object-C的任何对象都属于id类型),NULL则给任何指针赋值,NULL和nil不能互换,nil用于类指针赋值(在Object-C中类是一个对象,是类的meta-class的实例),而NSNull则用于集合操作,虽然它们表示的都是空值,但是使用场合完全不同,所以在编码时严格按照变量类型来赋值,将正确的空值赋给正确的类型,使代码易于阅读和维护,也不易引起错误。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//判断对象不空
if(object) {}

//判断对象为空
if(object == nil) {}

//数组初始化,空值结束
NSArray *pageNames=[[NSArray alloc] initWithObjects:@"DocumentList",@"AdvancedSearch",@"Statistics",nil];

//判断数组元素是否为空
UIViewController *controller=[NSArray objectAtIndex:i];
if((NSNull *)controller == [NSNull null])
{
//
}

//判断字典对象的元素是否为空
NSString *userId=[NSDictionary objectForKey:@"UserId"];
if(userId == [NSNull null])
{
//
}

Object-C有个可爱的特性,就是当发消息给nil对象时,系统返回0值而不是引起异常,这和JAVA烦人的NullPointerException以及C/C++的程序直接崩溃的处理完全不一样,明白Object-C的这个特性对于开发正确的IOS程序非常重要,因为nil是对象的合法值,nil对象同样可以接收消息,例如:

1
2
3
person=nil;
[person castBallot];
NSLog("person=%@",person);

对象置空,然后发送消息,程序同样接着往下执行而不会崩溃。

0%