Gowhich

Durban's Blog

字符串比较

1
2
3
4
5
6
7
8
//字符串比较
NSString *string = @"hello nihao";
NSString *otherString = @"hello niyeyao";
if([string compare:otherString] == NSOrderedAscending){
NSLog(@"我比你大");
}else{
NSLog(@"我是小三");
}

日期比较

1
2
3
4
5
6
7
8
//日期比较
NSDate *nowDate = [NSDate date];
NSDate *yesterdayDate = [[NSDate alloc] initWithTimeIntervalSinceNow:-24 * 60 * 60];
if([nowDate compare:yesterdayDate] == NSOrderedAscending){
NSLog(@"我是今天");
}else{
NSLog(@"我是我昨天");
}

当然这里也可以,将指定的字符串转换为时间进行比较,字符串的时间格式要跟自己设定的时间格式相对应

1
2
3
4
5
6
7
8
9
10
11
12
13
//日期比较
NSString *nowDateString = @"2013/7/13";
NSString *yesterdayDateString = @"2013/7/12";
NSDateFormatter *dateFromatter = [[NSDateFormatter alloc] init];
[dateFromatter setDateFormat:@"yy/MM/dd"];
NSDate *nowDate = [dateFromatter dateFromString:nowDateString];
NSDate *yesterdayDate = [dateFromatter dateFromString:yesterdayDateString];

if([nowDate compare:yesterdayDate] == NSOrderedAscending){
NSLog(@"我是今天");
}else{
NSLog(@"我是我昨天");
}

结果他的表现就是

1
2
2013-07-20 18:18:03.165 寻艺[90532:c07] 我比你大
2013-07-20 18:18:03.168 寻艺[90532:c07] 我是我昨天

这里需要注意的就是

NSOrderedAscending

我们在Xcode定位的话

会找到这样的代码

1
typedef NS_ENUM(NSInteger, NSComparisonResult) {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending};

结果 跟 -1 0 1应该是一样的。

iOS 根据内容计算高度,网络上搜索到的,自己记录下,看到的就看到了

第一步属性声明

1
@property (strong, nonatomic) NSString *personProfile

第二步属性赋值

1
self.personProfile = @"xxxxxxxxxxxxxxxx x x x  x x xx x ";

第三步求出高度

1
2
3
4
5
6
//根据内容计算高度
CGSize lineSize = [self.personProfile sizeWithFont:[UIFont systemFontOfSize:14]
constrainedToSize:CGSizeMake(240, 2000)
lineBreakMode:UILineBreakModeWordWrap];

NSLog(@" lineSize.height = %f", lineSize.height);

关于GHUnit的使用,我是摘自一篇比较不错的文章,里面即讲到了Xcode本身自带的测试工具OCUnit,同时也介绍了GHUnit,看完后很是受益呀。

XCode 内置了 OCUnit 单元测试框架,但目前最好用的测试框架应该是 GHUnit。通过 GHUnit + OCMock 组合,我们可以在 iOS 下进行较强大的单元测试功能。本文将演示如何在 XCode 4.2 下使用 OCUnit, GHUnit 和 OCMock 进行单元测试。

OCUnit

在 XCode 下新建一个 OCUnitProject 工程,选中 Include Unit Tests 选择框,

OCUnit 框架则会为我们自动添加 Unit Test 框架代码:

XCode 在 OCUnitProjectTests.m 中为我们自动生成了一个 Fail 的测试:

1
2
3
4
- (void)testExample
{
STFail(@"Unit tests are not implemented yet in OCUnitProjectTests");
}

让我们来运行 Test,看看效果:

从图中的红色下划线部分可以看出,测试没有通过,符合预期。我们只要像类 OCUnitProjectTests 一样编写继承自 SenTestCase 类的子类,在其中添加形式如:- (void) testXXX(); 的测试函数既可,注意必须是一个无参无返回类型且名称是以 test 为前缀的函数。

OCUnit 的有点是官方支持,于 XCode 集成的比较好。

GHUnit

GHUnit 是一个开源的单元测试框架,具有可视化界面,功能亦相当强大。Mark 写了一篇 OCUnit vs GHUnit 的文章,有兴趣的童鞋可以看一看。OCMock 是由 Mulle Kybernetik 为 OS X 和 iOS 平台编写的遵循 mock object 理念的单元测试框架。

下面来介绍如何配置 GHUnit 和 OCMock

1,首先,创建一个名为 GHUnitProject 的单视图应用程序,注意:不要选中 Include Unit Tests 选择框。然后运行,应该出现白屏。

2,添加新的 test target,选中左边的工程名,点击右侧的 Add Target,新增一个名为 Tests 的 Empty Application 应用程序,让其附属于 GHUnitProject注意:不要选中 Include Unit Tests 选择框。

3,向 Tests 工程中(注意是 Tests 工程)添加 GHUnitIOS Framework。首先下载与 XCode 版本对应的 GHUnitIOS Framework。英文好的可以直接查看官方 iOS 版的安装文档:点此查看,跳过此第 3 节;否则请接着看。

3.1,解压 GHUnitIOS 框架到 GHUnitProject 下,让 GHUnitIOS.framework 与 Tests 在同一目录下。

3.2,回到 XCode,右击工程中的 Frameworks group,选中 Add Files to…菜单,选取 GHUnitIOS.framework ,注意 targets 要选择 Tests。

3.3,设置 Tests 的 Build Settings:在 Other Linker Flags 中增加两个 flag: -ObjC 和 -all_load。

3.4,删除 Tests 工程中的 UTSAppDelegate.h 和 UTSAppDelegate.m 两个文件;

3.5,修改 Tests 工程中的 main.m 为:

1
2
3
4
5
6
7
8
9
10
#import <UIKit/UIKit.h>

#import <GHUnitIOS/GHUnitIOSAppDelegate.h>

int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([GHUnitIOSAppDelegate class]));
}
}

3.6,选择编译目标 Tests>iPhone 5.0 Simulator,编译运行,应该能得到如下效果。目前我们还没有编写任何实际测试,所以列表为空。

4,编写 GHUnit 测试。向 Tests 工程中添加名为 GHUnitSampleTest 的 Objective C class。其内容如下:

GHUnitSampleTest.h
1
2
3
4
5
6
7
8
9
#import <GHUnitIOS/GHUnit.h>

@interface GHUnitSampleTest: GHTestCase

{

}

@end
GHUnitSampleTest.m
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

#import "GHUnitSampleTest.h"

@implementation GHUnitSampleTest

- (void)testStrings

{

NSString *string1 = @"a string";

GHTestLog(@"I can log to the GHUnit test console: %@", string1);

// Assert string1 is not NULL, with no custom error description

GHAssertNotNULL(string1, nil);

// Assert equal objects, add custom error description

NSString *string2 = @"a string";

GHAssertEqualObjects(string1, string2, @"A custom error message. string1 should be equal to: %@.", string2);

}

@end

然后编译运行,点击 Run,效果如下:

图中的 All 栏显示所以的测试,Failed 栏显示没有通过的测试。强大吧,GHUnit。你可以向 GHUnitSampleTest 添加新的测试,比如:

1
2
3
4
5
6
7
- (void)testSimpleFail

{

GHAssertTrue(NO, nil);

}

我们可以向 Tests 添加更多测试类,只要该类是继承自 GHTestCase,且其中的测试方法都是无参无返回值且方法名字是以 test 为前缀即可

是不是有种感觉就是,以后再多的bug都不怕啦。哦也

关于这个问题,也是一直困扰我很久的问题了,一直记恨于心啊,终于今天让我知道了,如何解决这个问题啦,哈哈,大笑江湖呀。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *rootVC;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
if(![[NSUserDefaults standardUserDefaults] boolForKey:@"logged_in"]) {
rootVC=[self.storyboard instantiateViewControllerWithIdentifier: @"vc1"];
} else {
rootVC=[self.storyboard instantiateViewControllerWithIdentifier: @"vc2"];
}
window.rootViewConroller=rootVC;
[self.window makeKeyAndVisible];
return YES;
}

这里面的“vc1”和“vc2”,其实就是storyboard的表示符,自己在创建storyboard的时候一般都会看到的,如果自己还不知道的话,多创建几个自己就知道了。

关于“ios Application tried to push a nil view controller on target”这个错误,原因其实很简单,问题都是找到了才觉得简单,处理的时候不知道有多烦恼。呵呵,本来就应该是这样吧。

没有错误的作法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

rootViewController = [[WalkerUITestViewController alloc] initWithNibName:@"WalkerUITestViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];


self.window.rootViewController = navigationController;



self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

有错误的作法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

navigationController = [[UINavigationController alloc]
initWithRootViewController:rootViewController];


self.window.rootViewController = navigationController;



self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

当然在这两个例子里面,属性的声明是一样的

1
2
@synthesize navigationController;
@synthesize rootViewController;

其实就是在使用之前,自己有木有初始化的问题。ok。

关于这篇文章,要源于自己的一个测试,因为在一边学ios开发和学习的过程中,遇到一个问题就是做测试,但是测试的时候不知道为什么老是提示,没有test单元什么的错误,回到家,好好的琢磨了一下,发现了,这个是测试单元是可以自己创建的,我这里转载的一篇文章,来csdn,里面说的很详细,是如何进行测试单元的创建,的同时也解决了我的一个问题,就是“Unit tests are not implemented yet in WalkerUITests”这个问题,呵呵,笑死我啦,其实这个是一个测试单元建立成功的提示,接下来,做这个测试就很容易啦。

1、创建单元测试的target

选择工程,点击Add Target,添加ios-Other下的Cocoa Touch Unit Testing Bundle类型target。(网上一些资料说,target后缀必须是Tests,但实际测试同名字无关系,可能是Xcode版本原因)。

此时工程目录下会多一个Unistest8文件夹,Unistest8类是一个测试用例类Test8的一个实例。

2、添加SenTestingKit.framework

选中测试的Target,本项目中即Unitest8,查看Build Phases选项卡下的Link Binary With Libraries,会发现项目中缺少对SenTestingKit.framework库的引用,将其添加。

3、运行Unistest8测试

在Scheme中选择Unitest8,生成时一定要选择Product-Test,如果选择Run,则会出现“The scheme ‘Unitest8’ is not configured for Running”的错误。

成功运行后,发现输出错误,错误信息如下:

1
2
3
4
5
Unitest8.m:29: error: -[Unitest8 testExample] : Unit tests are not implemented       yet in Unitest8    



Test Case '-[Unitest8 testExample]' failed (0.057 seconds).

打开Unitest8.m,会发现在Unitest8类的实现文件中默认添加了一个测试函数testExample,并添加一个执行失败的提示,如下:

1
2
3
- (void)testExample{    
STFail(@"Unit tests are not implemented yet in Unitest8");
}

看到这个错误,整个测试的准备工作已经完成,接下来就可以在测试用例的类实例中添加测试函数,测试函数的名称就不能随意了,必须以test为前缀。在测试文件夹中还可以添加多个测试用例进行测试。

4、添加测试对象类,进行测试

完成步骤3后,可以开始对项目中具体的类进行方法测试。除了在相应的测试用例中添加该类的引用外,首先得将这个类引用加入该测试的Target(很2地因为这个问题卡了许久),在本项目里即为Unitest8。添加引用的方式是,点击测试target(),选择面板Build Phases,打开Compile Sources,选中项目中要测试类的.m实现文件将其拖入Compile Sources就ok。否则会产生如下error:

1
2
3
4
Undefined symbols for architecture i386:    
"_OBJC_CLASS_$_测试类名", referenced from:
objc-class-ref inUnitest8.o
ld: symbol(s) not found for architecture i386

如果测试的类存在nib文件则需要将nib文件拖入Build Phases–Copy Bundle Resources中。

以上就是参考的文章的主要内容,这里介绍给大家,希望对自己有用,对自己也有用。

参考文章:http://blog.csdn.net/catandrat111/article/details/7819284

先看两个例子:

第一个例子,代码如下:

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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
-(void) setPersonInfo:(CGFloat)x y:(CGFloat)y heihgt:(CGFloat)height{
CGFloat width = self.view.frame.size.width - 2 * x;
UILabel *labelInfoArea = [[UILabel alloc]initWithFrame:CGRectMake(x, y+10.0, width, height)];
labelInfoArea.backgroundColor = [UIColor clearColor];
[_contentScroll addSubview:labelInfoArea];

//基本信息的标题
UILabel *labelInfoCaption = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, labelInfoArea.frame.size.width * 0.5, 30.0)];
labelInfoCaption.textColor = [UIColor blackColor];
labelInfoCaption.text = @"基本信息";
labelInfoCaption.font = [UIFont systemFontOfSize:16.0];
labelInfoCaption.backgroundColor = [UIColor clearColor];
[labelInfoArea addSubview:labelInfoCaption];

//complete button
if([[NSString stringWithFormat:@"%@",[_personInfo valueForKey:@"is_completed"]] isEqualToString:@"0"]){
UIButton *completeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
completeBtn.layer.borderColor = [[UIColor colorWithRed:161.0/255.0 green:159.0/255.0 blue:160.0/255.0 alpha:1.0] CGColor];
completeBtn.layer.borderWidth = 1.0;
completeBtn.layer.cornerRadius = 5.0;
completeBtn.layer.backgroundColor = [[UIColor colorWithRed:161.0/255.0 green:159.0/255.0 blue:160.0/255.0 alpha:1.0] CGColor];
[completeBtn setTitle:@"我来完善" forState:UIControlStateNormal];
completeBtn.titleLabel.font = [UIFont systemFontOfSize:12.0];
[completeBtn addTarget:self
action:@selector(redirectCompleteDetail)
forControlEvents:UIControlEventTouchUpInside];
[completeBtn setFrame:CGRectMake(self.view.frame.size.width - 80.0, y + 15.0, 60.0, 20.0)];
[_contentScroll addSubview:completeBtn];
}

//基本信息表格
UILabel *labelInfoTable = [[UILabel alloc] initWithFrame:CGRectMake(labelInfoCaption.frame.origin.x, labelInfoCaption.frame.origin.x + labelInfoCaption.frame.size.height, labelInfoArea.frame.size.width, height - 30.0)];
labelInfoTable.backgroundColor = [UIColor lightGrayColor];
labelInfoTable.layer.borderColor = [[UIColor lightGrayColor] CGColor];
labelInfoTable.layer.borderWidth = 1.0;
labelInfoTable.layer.cornerRadius = 10.0;
[labelInfoArea addSubview:labelInfoTable];

CGFloat tableCellHeight = labelInfoTable.frame.size.height / 2;
CGFloat tableCellWidth = labelInfoTable.frame.size.width / 4;

//添加基本信息-年龄
UILabel *labelAge = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, tableCellWidth, tableCellHeight)];
labelAge.backgroundColor = [UIColor whiteColor];
labelAge.text = @"年龄";
labelAge.font = [UIFont systemFontOfSize:14];
labelAge.textAlignment = NSTextAlignmentCenter;
[labelInfoTable addSubview:labelAge];

//添加基本信息-身高
UILabel *labelHeight = [[UILabel alloc] initWithFrame:CGRectMake(labelAge.frame.origin.x + labelAge.frame.size.width, 0.0, labelAge.frame.size.width, labelAge.frame.size.height)];
labelHeight.backgroundColor = [UIColor whiteColor];
labelHeight.text = @"身高";
labelHeight.font = [UIFont systemFontOfSize:14];
labelHeight.textAlignment = NSTextAlignmentCenter;
[labelInfoTable addSubview:labelHeight];

//添加基本信息-体重
UILabel *labelWeight = [[UILabel alloc] initWithFrame:CGRectMake(labelHeight.frame.origin.x + labelHeight.frame.size.width, 0.0, labelHeight.frame.size.width, labelHeight.frame.size.height)];
labelWeight.backgroundColor = [UIColor whiteColor];
labelWeight.text = @"体重";
labelWeight.font = [UIFont systemFontOfSize:14];
labelWeight.textAlignment = NSTextAlignmentCenter;
[labelInfoTable addSubview:labelWeight];

//添加基本信息-性别
UILabel *labelBMI = [[UILabel alloc] initWithFrame:CGRectMake(labelWeight.frame.origin.x + labelWeight.frame.size.width, 0.0, labelHeight.frame.size.width, labelHeight.frame.size.height)];
labelBMI.backgroundColor = [UIColor whiteColor];
labelBMI.text = @"性别";
labelBMI.font = [UIFont systemFontOfSize:14];
labelBMI.textAlignment = NSTextAlignmentCenter;
[labelInfoTable addSubview:labelBMI];


//+++++++++++++++++++
//添加基本信息-年龄值
UILabel *labelAgeValue = [[UILabel alloc] initWithFrame:CGRectMake(0.0, labelInfoTable.frame.size.height / 2, labelInfoTable.frame.size.width / 4, labelInfoTable.frame.size.height / 2)];
labelAgeValue.backgroundColor = [UIColor whiteColor];
labelAgeValue.font = [UIFont systemFontOfSize:14];
if([[NSString stringWithFormat:@"%@",[self.personInfo valueForKey:@"age"]] isEqualToString:@"0"])
{
labelAgeValue.text = @"无";
}
else
{
labelAgeValue.text = [NSString stringWithFormat:@"%@岁",[self.personInfo valueForKey:@"age"]];
}

labelAgeValue.textAlignment = NSTextAlignmentCenter;
[labelInfoTable addSubview:labelAgeValue];

//添加基本信息-身高值
UILabel *labelHeightValue = [[UILabel alloc] initWithFrame:CGRectMake(labelAgeValue.frame.origin.x + labelAgeValue.frame.size.width, labelAgeValue.frame.origin.y, labelAgeValue.frame.size.width, labelAgeValue.frame.size.height)];
labelHeightValue.backgroundColor = [UIColor whiteColor];
labelHeightValue.font = [UIFont systemFontOfSize:14];
if([[NSString stringWithFormat:@"%@",[self.personInfo valueForKey:@"height"]] isEqualToString:@"0"])
{
labelHeightValue.text = @"无";
}
else
{
labelHeightValue.text = [NSString stringWithFormat:@"%@CM",[self.personInfo valueForKey:@"height"]];
}

labelHeightValue.textAlignment = NSTextAlignmentCenter;
[labelInfoTable addSubview:labelHeightValue];

//添加基本信息-体重值
UILabel *labelWeightValue = [[UILabel alloc] initWithFrame:CGRectMake(labelHeightValue.frame.origin.x + labelHeightValue.frame.size.width, labelHeightValue.frame.origin.y, labelHeightValue.frame.size.width, labelHeightValue.frame.size.height)];
labelWeightValue.backgroundColor = [UIColor whiteColor];
labelWeightValue.font = [UIFont systemFontOfSize:14];
if([[NSString stringWithFormat:@"%@",[self.personInfo valueForKey:@"weight"]] isEqualToString:@"0"])
{
labelWeightValue.text = @"无";
}
else
{
labelWeightValue.text = [NSString stringWithFormat:@"%@KG",[self.personInfo valueForKey:@"weight"]];
}


labelWeightValue.textAlignment = NSTextAlignmentCenter;
[labelInfoTable addSubview:labelWeightValue];

//添加基本信息-性别
UILabel *labelBMIValue = [[UILabel alloc] initWithFrame:CGRectMake(labelWeightValue.frame.origin.x + labelWeightValue.frame.size.width, labelWeightValue.frame.origin.y, labelWeightValue.frame.size.width, labelWeightValue.frame.size.height)];
labelBMIValue.backgroundColor = [UIColor whiteColor];
labelBMIValue.font = [UIFont systemFontOfSize:14];
if([[NSString stringWithFormat:@"%@",[self.personInfo valueForKey:@"sex"]] isEqualToString:@"0"])
{
labelBMIValue.text = @"无";
}
else
{
labelBMIValue.text = [NSString stringWithFormat:@"%@",[self.personInfo valueForKey:@"sex"]];
}
//
// if([[NSString stringWithFormat:@"%@",[self.personInfo valueForKey:@"bmi"]] isEqualToString:@"0"])
// {
// labelBMIValue.text = @"无";
// }
// else
// {
// labelBMIValue.text = [NSString stringWithFormat:@"%@KG",[self.personInfo valueForKey:@"bmi"]];
// }

labelBMIValue.textAlignment = NSTextAlignmentCenter;
[labelInfoTable addSubview:labelBMIValue];
//++++++++++++++++

//横轴分割线-第一根
UILabel *vfirstLine = [[UILabel alloc] initWithFrame:CGRectMake(0.0, tableCellHeight + labelAge.frame.origin.y, labelInfoTable.frame.size.width, 1.0)];
vfirstLine.backgroundColor = [UIColor lightGrayColor];
[labelInfoTable addSubview:vfirstLine];

//竖轴分割线-第一根
UILabel *firstLine = [[UILabel alloc] initWithFrame:CGRectMake(labelAge.frame.origin.x + labelAge.frame.size.width, 0.0, 1.0, labelInfoTable.frame.size.height)];
firstLine.backgroundColor = [UIColor lightGrayColor];
[labelInfoTable addSubview:firstLine];

//竖轴分割线-第二根
UILabel *secondLine = [[UILabel alloc] initWithFrame:CGRectMake(labelHeight.frame.origin.x + labelHeight.frame.size.width, 0.0, 1.0, labelInfoTable.frame.size.height)];
secondLine.backgroundColor = [UIColor lightGrayColor];
[labelInfoTable addSubview:secondLine];

//竖轴分割线-第三根
UILabel *thridLine = [[UILabel alloc] initWithFrame:CGRectMake(labelWeight.frame.origin.x + labelWeight.frame.size.width, 0.0, 1.0, labelInfoTable.frame.size.height)];
thridLine.backgroundColor = [UIColor lightGrayColor];
[labelInfoTable addSubview:thridLine];
}

第二个例子,代码如下:

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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
-(void) setPersonInfo:(CGFloat)x y:(CGFloat)y heihgt:(CGFloat)height{
CGFloat width = self.view.frame.size.width - 2 * x;
UILabel *labelInfoArea = [[UILabel alloc]initWithFrame:CGRectMake(x, y+10.0, width, height)];
labelInfoArea.backgroundColor = [UIColor clearColor];
[_contentScroll addSubview:labelInfoArea];

//基本信息的标题
UILabel *labelInfoCaption = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, labelInfoArea.frame.size.width * 0.5, 30.0)];
labelInfoCaption.textColor = [UIColor blackColor];
labelInfoCaption.text = @"基本信息";
labelInfoCaption.font = [UIFont systemFontOfSize:16.0];
labelInfoCaption.backgroundColor = [UIColor clearColor];
[labelInfoArea addSubview:labelInfoCaption];

//基本信息表格
UILabel *labelInfoTable = [[UILabel alloc] initWithFrame:CGRectMake(labelInfoCaption.frame.origin.x, labelInfoCaption.frame.origin.x + labelInfoCaption.frame.size.height, labelInfoArea.frame.size.width, height - 30.0)];
labelInfoTable.backgroundColor = [UIColor lightGrayColor];
labelInfoTable.layer.borderColor = [[UIColor lightGrayColor] CGColor];
labelInfoTable.layer.borderWidth = 1.0;
labelInfoTable.layer.cornerRadius = 10.0;
[labelInfoArea addSubview:labelInfoTable];

CGFloat tableCellHeight = labelInfoTable.frame.size.height / 2;
CGFloat tableCellWidth = labelInfoTable.frame.size.width / 4;

//添加基本信息-年龄
UILabel *labelAge = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, tableCellWidth, tableCellHeight)];
labelAge.backgroundColor = [UIColor whiteColor];
labelAge.text = @"年龄";
labelAge.font = [UIFont systemFontOfSize:14];
labelAge.textAlignment = NSTextAlignmentCenter;
[labelInfoTable addSubview:labelAge];

//添加基本信息-身高
UILabel *labelHeight = [[UILabel alloc] initWithFrame:CGRectMake(labelAge.frame.origin.x + labelAge.frame.size.width, 0.0, labelAge.frame.size.width, labelAge.frame.size.height)];
labelHeight.backgroundColor = [UIColor whiteColor];
labelHeight.text = @"身高";
labelHeight.font = [UIFont systemFontOfSize:14];
labelHeight.textAlignment = NSTextAlignmentCenter;
[labelInfoTable addSubview:labelHeight];

//添加基本信息-体重
UILabel *labelWeight = [[UILabel alloc] initWithFrame:CGRectMake(labelHeight.frame.origin.x + labelHeight.frame.size.width, 0.0, labelHeight.frame.size.width, labelHeight.frame.size.height)];
labelWeight.backgroundColor = [UIColor whiteColor];
labelWeight.text = @"体重";
labelWeight.font = [UIFont systemFontOfSize:14];
labelWeight.textAlignment = NSTextAlignmentCenter;
[labelInfoTable addSubview:labelWeight];

//添加基本信息-性别
UILabel *labelBMI = [[UILabel alloc] initWithFrame:CGRectMake(labelWeight.frame.origin.x + labelWeight.frame.size.width, 0.0, labelHeight.frame.size.width, labelHeight.frame.size.height)];
labelBMI.backgroundColor = [UIColor whiteColor];
labelBMI.text = @"性别";
labelBMI.font = [UIFont systemFontOfSize:14];
labelBMI.textAlignment = NSTextAlignmentCenter;
[labelInfoTable addSubview:labelBMI];


//+++++++++++++++++++
//添加基本信息-年龄值
UILabel *labelAgeValue = [[UILabel alloc] initWithFrame:CGRectMake(0.0, labelInfoTable.frame.size.height / 2, labelInfoTable.frame.size.width / 4, labelInfoTable.frame.size.height / 2)];
labelAgeValue.backgroundColor = [UIColor whiteColor];
labelAgeValue.font = [UIFont systemFontOfSize:14];
if([[NSString stringWithFormat:@"%@",[self.personInfo valueForKey:@"age"]] isEqualToString:@"0"])
{
labelAgeValue.text = @"无";
}
else
{
labelAgeValue.text = [NSString stringWithFormat:@"%@岁",[self.personInfo valueForKey:@"age"]];
}

labelAgeValue.textAlignment = NSTextAlignmentCenter;
[labelInfoTable addSubview:labelAgeValue];

//添加基本信息-身高值
UILabel *labelHeightValue = [[UILabel alloc] initWithFrame:CGRectMake(labelAgeValue.frame.origin.x + labelAgeValue.frame.size.width, labelAgeValue.frame.origin.y, labelAgeValue.frame.size.width, labelAgeValue.frame.size.height)];
labelHeightValue.backgroundColor = [UIColor whiteColor];
labelHeightValue.font = [UIFont systemFontOfSize:14];
if([[NSString stringWithFormat:@"%@",[self.personInfo valueForKey:@"height"]] isEqualToString:@"0"])
{
labelHeightValue.text = @"无";
}
else
{
labelHeightValue.text = [NSString stringWithFormat:@"%@CM",[self.personInfo valueForKey:@"height"]];
}

labelHeightValue.textAlignment = NSTextAlignmentCenter;
[labelInfoTable addSubview:labelHeightValue];

//添加基本信息-体重值
UILabel *labelWeightValue = [[UILabel alloc] initWithFrame:CGRectMake(labelHeightValue.frame.origin.x + labelHeightValue.frame.size.width, labelHeightValue.frame.origin.y, labelHeightValue.frame.size.width, labelHeightValue.frame.size.height)];
labelWeightValue.backgroundColor = [UIColor whiteColor];
labelWeightValue.font = [UIFont systemFontOfSize:14];
if([[NSString stringWithFormat:@"%@",[self.personInfo valueForKey:@"weight"]] isEqualToString:@"0"])
{
labelWeightValue.text = @"无";
}
else
{
labelWeightValue.text = [NSString stringWithFormat:@"%@KG",[self.personInfo valueForKey:@"weight"]];
}


labelWeightValue.textAlignment = NSTextAlignmentCenter;
[labelInfoTable addSubview:labelWeightValue];

//添加基本信息-性别
UILabel *labelBMIValue = [[UILabel alloc] initWithFrame:CGRectMake(labelWeightValue.frame.origin.x + labelWeightValue.frame.size.width, labelWeightValue.frame.origin.y, labelWeightValue.frame.size.width, labelWeightValue.frame.size.height)];
labelBMIValue.backgroundColor = [UIColor whiteColor];
labelBMIValue.font = [UIFont systemFontOfSize:14];
if([[NSString stringWithFormat:@"%@",[self.personInfo valueForKey:@"sex"]] isEqualToString:@"0"])
{
labelBMIValue.text = @"无";
}
else
{
labelBMIValue.text = [NSString stringWithFormat:@"%@",[self.personInfo valueForKey:@"sex"]];
}
//
// if([[NSString stringWithFormat:@"%@",[self.personInfo valueForKey:@"bmi"]] isEqualToString:@"0"])
// {
// labelBMIValue.text = @"无";
// }
// else
// {
// labelBMIValue.text = [NSString stringWithFormat:@"%@KG",[self.personInfo valueForKey:@"bmi"]];
// }

labelBMIValue.textAlignment = NSTextAlignmentCenter;
[labelInfoTable addSubview:labelBMIValue];
//++++++++++++++++

//横轴分割线-第一根
UILabel *vfirstLine = [[UILabel alloc] initWithFrame:CGRectMake(0.0, tableCellHeight + labelAge.frame.origin.y, labelInfoTable.frame.size.width, 1.0)];
vfirstLine.backgroundColor = [UIColor lightGrayColor];
[labelInfoTable addSubview:vfirstLine];

//竖轴分割线-第一根
UILabel *firstLine = [[UILabel alloc] initWithFrame:CGRectMake(labelAge.frame.origin.x + labelAge.frame.size.width, 0.0, 1.0, labelInfoTable.frame.size.height)];
firstLine.backgroundColor = [UIColor lightGrayColor];
[labelInfoTable addSubview:firstLine];

//竖轴分割线-第二根
UILabel *secondLine = [[UILabel alloc] initWithFrame:CGRectMake(labelHeight.frame.origin.x + labelHeight.frame.size.width, 0.0, 1.0, labelInfoTable.frame.size.height)];
secondLine.backgroundColor = [UIColor lightGrayColor];
[labelInfoTable addSubview:secondLine];

//竖轴分割线-第三根
UILabel *thridLine = [[UILabel alloc] initWithFrame:CGRectMake(labelWeight.frame.origin.x + labelWeight.frame.size.width, 0.0, 1.0, labelInfoTable.frame.size.height)];
thridLine.backgroundColor = [UIColor lightGrayColor];
[labelInfoTable addSubview:thridLine];
}


//完善信息的按钮
-(void) setupComplateButton:(CGFloat) y{
//基本信息的完善按钮
if([[NSString stringWithFormat:@"%@",[_personInfo valueForKey:@"is_completed"]] isEqualToString:@"0"]){
UIButton *completeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
completeBtn.layer.borderColor = [[UIColor colorWithRed:161.0/255.0 green:159.0/255.0 blue:160.0/255.0 alpha:1.0] CGColor];
completeBtn.layer.borderWidth = 1.0;
completeBtn.layer.cornerRadius = 5.0;
completeBtn.layer.backgroundColor = [[UIColor colorWithRed:161.0/255.0 green:159.0/255.0 blue:160.0/255.0 alpha:1.0] CGColor];
[completeBtn setTitle:@"我来完善" forState:UIControlStateNormal];
completeBtn.titleLabel.font = [UIFont systemFontOfSize:12.0];
[completeBtn addTarget:self
action:@selector(redirectCompleteDetail)
forControlEvents:UIControlEventTouchUpInside];
[completeBtn setFrame:CGRectMake(self.view.frame.size.width - 80.0, y + 15.0, 60.0, 20.0)];
[_contentScroll addSubview:completeBtn];
}
}

第一个例子,我将按钮直接放在了里面。导致我后面做操作的时候,似乎将重新添加了一个层,将我的UIbutton按钮覆盖掉了。导致不起作用。第二个例子是我在这个项目的解决方案,直接分开,我将UIButton直接作为最后的层添加到view上,这样就不会覆盖掉,结果达到了我想要的效果。

一款可以让你开发效率提高10陪的DIY神器
关于这篇文章,我看完后,感觉确实不错,虽然我用的是MacVim。但是感觉还是差不多的。

对于学习php同学应该会很有帮助的。

终端下执行命令:whereis vim 将列出vim安装的路径。
否则执行 sudo apt-get install vim 安装vim 。
成功安装了vim,只需要在用户根目录下创建.vimrc文件,在配置文件下写入如下信息。
比如:
“引号代表注释

set hlsearch “高亮度反白
set backspace=2 “可随时用倒退键删除
set autoindent “自动缩排
set ruler “可显示最后一行的状态
set showmode “左下角那一行的状态
set nu “可以在每一行的最前面显示行号
set bg=dark “显示不同的底色色调
syntax on “进行语法检验,颜色显示
set wrap “自动折行
set shiftwidth=4
set tabstop=4
set softtabstop=4
set expandtab “将tab替换为相应数量空格
set smartindent

######下面可根据自己的需要,可以不选用#############
set guifont=Dorid Sans Mono:h14:uft8 “gvim字体设置
set encoding=utf8 “设置内部编码为utf8
set fileencoding=utf8 “当前编辑的文件编码
set fileencodings=uft8-bom,utf8,gbk,gb2312,big5 “打开支持编码的文件

“解决consle输出乱码
language messages zh_CN.utf-8
“解决菜单乱码
source $VIMRUNTIME/delmenu.vim
source $VIMRUNTIME/menu.vim

一、如何安装phpcomplete插件
如果是VIM7.0以上,不需要再下载 phpcomplete.vim 这个插件,因为安装时自带了,在目录/usr/share/vim/vim73/autoload/phpcomplete.vim中。
在 ~/.vimrc 中添加这样两行:
filetype plugin on
autocmd FileType php set omnifunc=phpcomplete#CompletePHP

如何使用:
vi index.php
插入一段php代码后比如:
htmlsp
先按下 Ctrl+x进入^X模式,再按下 Ctrl+o, 就能看到提示列表框,以及对应的function,还有对应的函数定义比如参数等等
Ctrl+n, Ctrl+p 来上下选择,ESC 来取消提示

二、如何安装php documentor插件
http://www.vim.org/scripts/script.php?script\_id=1355
根据官网提供的安装实例,我们进行以下操作:
下载php-doc.vim
cp ./php-doc.vim /usr/share/vim/vim73/autoload/php-doc.vim

install details
Installation

For example include into your .vimrc:

vi ~.vimrc

source /usr/share/vim/vim73/autoload/php-doc.vim
inoremap :call PhpDocSingle()i
nnoremap :call PhpDocSingle()
vnoremap :call PhpDocRange()

如何使用:
在函数定义出注释按ctrl+p即可
[attachment=28886]

三、如何安装NERDTree插件
http://www.vim.org/scripts/script.php?script\_id=1658
然后解压,解压缩后把plugin,doc,syntax,nerdtree_plugin四个目录复制到/usr/share/vim/vim73/目录下,即可完成安装。
进入vim后 :NERDTree开启

如何使用
1、在终端界面,输入vim
2、输入 :NERDTree ,回车
3、进入当前目录的树形界面,通过h,j键或者小键盘上下键,能移动选中的目录或文件
4、按u键到上级目录,按o键打开或者关闭文件。目录前面有+号,摁Enter会展开目录,文件前面是-号,摁Enter会在右侧窗口展现该文件的内容,并光标的焦点focus右侧。
5、ctr+w+h 光标focus左侧树形目录,ctrl+w+l 光标focus右侧文件显示窗口。多次摁 ctrl+w,光标自动在左右侧窗口切换
6、光标focus左侧树形窗口,摁? 弹出NERDTree的帮助,再次摁?关闭帮助显示
7、输入:q回车,关闭光标所在窗口

四、如何安装neocomplcache代码自动补全函数提示(支持C/C++,java,python,PHP,javascrip众多语言 )
http://www.vim.org/scripts/script.php?script\_id=2620
然后解压,解压缩后把autoload,plugin,doc三个目录复制到/usr/share/vim/vim73/

添加一下内容到~/.vimrc文件中
if &term==”xterm”
set t_Co=8
set t_Sb=^[[4%dm
set t_Sf=^[[3%dm
endif
let g:neocomplcache_enable_at_startup = 1

五、如何安装zencodeing 引起美工业内13级地震的超级利器
http://www.vim.org/scripts/script.php?script\_id=2981 下载得到
解压缩后把三个目录复制到/usr/share/vim/vim73/

方法二
在用户根目录下创建~/ .vim文件夹 ,将加压后得到的三个目录放入此文件夹即可。
测试是否安装成功 :
输入 html:4s
按住Ctrl 再按下 “y” 和“,” 看到发生了什么?震惊了吗??

NSNotification广播实现视图跳转传递数据
广播机制分为:注册->发送->接收(接收方)
第一步,在要发送数据的视图页面.m文件处理发送逻辑的方法里注册+发送

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- (IBAction)pressed:(id)sender {

// [self performSegueWithIdentifier:@"second" sender:self];
NSLog(@"send message:%@",firstField.text);



//页面跳转传值方法二:利用notification
NSDictionary *dicts = [NSDictionary dictionaryWithObjectsAndKeys:@"one1",@"one",@"two2",@"two",@"three3",@"three", nil];
//注册(第一步)
NSNotification *notification =[NSNotification notificationWithName:@"mynotification" object:firstField.text];
//发送(第二步)
[[NSNotificationCenter defaultCenter] postNotification:notification];

//注册+发送也可以一行完成(等效于以上两行)
[[NSNotificationCenter defaultCenter] postNotificationName:@"mynotification2" object:dicts];//发送一个字典过去
}

notificationWithName:参数的值是自己定义,接收方以此名称为接收标识。
第二步,在跳转后,接收数据视图页面.m文件中处理逻辑的方法里 接收

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
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

//接受端:接受(第一步)

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(notificationHandler:) name:@"mynotification"
object:nil];


[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(notificationHandler2:) name:@"mynotification2"
object:nil];

}
//自定义接收信息和处理的方法(第二步)
-(void) notificationHandler:(NSNotification *) notification{

secondField.text = [notification object];//收到消息后在UItextField中显示出来

}
//自定义接收字典信息的方法
-(void) notificationHandler2:(NSNotification *) notification2{

NSDictionary *dict = [notification2 object];
NSLog(@"receive dict :%@,forkey:%@",dict,[dict objectForKey:@"one"]);

}

注意:如果注册的notification在目标视图没有收到或名称写错,目标视图的相关方法就不会执行

0%