您的位置:首页 > 其它

几个常用控件的笔记

2012-09-06 16:25 267 查看
二、UIPickerView控件
1.必须在controller中实现PickerView的delegate和数据源DataSource协议。以pickerView开头的方法有6个(头文件引入协议名后可在codesense看到)。在这些方法中,告诉选取器显示多少组件,每个组件各有所少行,组件的宽度,各显示什么数据或是图像,以及每个组件选取时的事件(如reload依赖其的组件)。

1.1每个组件选取时的事件(如reload依赖其的组件)

-(void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)rowinComponent:(NSInteger)component{

if (kStateCompoment ==component) {

NSString*selectedState = [self.state
objectAtIndex:row];

NSArray*array =[self.stateZips
objectForKey:selectedState];

self.zips=array;

[picker
selectRow:0
inComponent:kZipsCompoment
animated:YES];

[pickerView reloadComponent:kZipsCompoment];

}

}

如果不需要让用户在选取器上操作,也可以用屏蔽掉用selectRow:方法自己选择,就像代替用户选择一样。



步骤:

A.在controller的头文件中添加委托名

@interfaceSinglerComponentPickerController :
UIViewController

<UIPickerViewDelegate,UIPickerViewDataSource>

B。实现协议方法

#pragmamark -

#pragmamark Picker Data Source Methods

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerVie{

return 1;

}

-(NSInteger)pickerView:(UIPickerView*)pickerView numberOfRowsInComponent:(NSInteger)component{

NSLog(@"count:%d",[pickerData
count]);

return [pickerData
count];

}

#pragmamark Picker View Methods

-(NSString*)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)rowforComponent:(NSInteger)component{

return [pickerData
objectAtIndex:row];

}

2、构建nib:要注意除了picker的输出口,还要指定实现Delegate和datasource的文件,也就是fileOwer。

3、在tabbarcontroller等低一层的controller里添加view controller并进行class设置。一个controller管一个视图

三、关于宏定义,我不得不写上这个来提醒你,不要给宏定义加分号!!!!!

四、一个封装好的弹出警告框的方法。

-(void)alert:(NSString*)msg{

NSString*title =[[NSString
alloc]initWithFormat:@"%@!",msg];

UIAlertView*alert = [[UIAlertView
alloc]initWithTitle:title message:@"Thankyou for chosing!"

delegate:nil

cancelButtonTitle:@"Great!"

otherButtonTitles:nil];

[alert show];

}

五、束NSBundle

什么是束,束是特定的文件类型,mainBundle:方法返回此程序象征的主束,束用于管理项目的文件夹资源。除了在InterfaceBuilder使用图片,假若想在代码引用资源,就要用束。束会帮你进行打包转移等,这样就不用自己管理了。真是贴心的设计呢。

以下代码导入plist文件到dictionary

NSBundle*bundle = [NSBundle
mainBundle ];

NSString*plistPath = [bundle
pathForResource:@"statedictionary"
ofType:@"plist"];

self.stateZips =[[NSDictionary
alloc]initWithContentsOfFile:plistPath];

六、老虎机小游戏

6.1载入图像 写个老虎机,使用picker载入图像并作为视图去显示,屏蔽用户选择改为按钮事件中进行,所以要在Interaction上屏蔽组件不被用户操纵,并且设置游戏胜利的条件。

//载入图像是和图像所在路径无关的额,以下代码,调试时是载入成功的,也就是说xcode自动载入了路径。

ViewDidLoad实现

UIImage*seven=[UIImage
imageNamed:@"seven"];

UIImage*bar=[UIImage
imageNamed:@"bar"];

UIImage*cherry=[UIImage
imageNamed:@"cherry"];

UIImage*crown=[UIImage
imageNamed:@"crown"];

UIImage*lemon=[UIImage
imageNamed:@"lemon"];

UIImage*apple=[UIImage
imageNamed:@"apple"];

for (int i=0;i<5;i++) {

UIImageView*sevenView = [[UIImageView
alloc]initWithImage:seven];

UIImageView*barView = [[UIImageView
alloc]initWithImage:bar];

UIImageView*cherryView = [[UIImageView
alloc]initWithImage:cherry];

UIImageView*crownView = [[UIImageView
alloc]initWithImage:crown];

UIImageView*lemonView = [[UIImageView
alloc]initWithImage:lemon];

UIImageView*appleView = [[UIImageView
alloc]initWithImage:apple];

NSArray *imageView =[[NSArray
alloc]initWithObjects:sevenView,barView,cherryView,crownView,lemonView,appleView,nil];

NSString*fieldName =[[NSString
alloc]initWithFormat:@"column%d",i];

[self setValue:imageViewforKey:fieldName];



6.2细节处理

导入声音:如果编译器没有自动链接AduioToolBox.framework库请手动导入。

//path的例子:NSString*path = [[NSBundle
mainBundle]pathForResource:@"Win"
ofType:@"wav"];



voidnewSound(NSString *path) {

SystemSoundIDsoundID;

AudioServicesCreateSystemSoundID((__bridge
CFURLRef)[NSURL
fileURLWithPath:path],&soundID);

AudioServicesPlaySystemSound(soundID);

}

6.3弹出警告框

-(void)alert:(NSString*)msg{

NSString*title =[[NSString
alloc]initWithFormat:@"%@!",msg];

UIAlertView*alert = [[UIAlertView
alloc]initWithTitle:title message:@"Thankyou for chosing!"

delegate:nil

cancelButtonTitle:@"Great!"

otherButtonTitles:nil];

[alert show];

}

三、UITableView表控件
3.1

表按是否分组和是否有索引有不同的叫法,但是不应该去做又有分组又有索引的表。

UITablerView的组件叫做UITanbleViewCell。里面再包含图像或是文本都可以的。

3.2控件导入项目基本流程

3.2.1生成h、m、xib文件,并在控件库中找到TableViewController,用TableView代替原来的View,记得设置TableView的delegate和dataSource与File's owner的联结

3.2.2 h文件加上<UITableViewDelegate,UITableViewDataSource>

3.2.3#pragma mark Data Source Methods返回行数

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

return [self.listData
count];

}



3.2.4 #pragma mark Data Source Methods实现返回cell的方法以及设置文本和图标或字体

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

static NSString*SimpleTablerID =@"SimpleTablerID";

UITableViewCell*cell = [tableView
dequeueReusableCellWithIdentifier:SimpleTablerID];

if(nil==cell){

cell = [[UITableViewCell
alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTablerID];



}

NSUIntegerrow=[indexPath
row];

cell.textLabel.text=[listData
objectAtIndex:row];

UIImage*image=[UIImage
imageNamed:@"star"];

cell.imageView.image=image;

return cell;

}

3.2.5#pragma mark Delegate Methods

以下代码使5行cell为一个循环缩进级别,0就是不缩进了。

-(NSInteger)tableView:(UITableView*)tableView indentationLevelForRowAtIndexPath:(NSIndexPath*)indexPath{

return[indexPath
row]%5;

}

行选择的干预,以下代码使第一和最后一行不能选择

-(NSIndexPath*)tableView:(UITableView *)tableViewwillSelectRowAtIndexPath:(NSIndexPath *)indexPath{

NSIntegerrow=[indexPath
row];

if (0==row||([listData
count]-1)==row){

return
nil;

}

returnindexPath;

}



(点击)选择行后的事件处理

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{

NSIntegerrow=[indexPath
row];

NSString *msg=[[NSString
alloc]initWithFormat:@"Youselected %@",[listData
objectAtIndex:row]];

[self alert:msg];

}

返回行高的像素数

-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{

return 180;

}

四、UITableViewCell表控件介绍和自定义


4.1cell是TableView的组成部分,包含textLabel,imageView,accessoryType这些属性

4.2Cell的 accessoryType

The type of standard accessory view the cell should use(normal state).

@property(nonatomic) UITableViewCellAccessoryTypeaccessoryType

很幸运IB是支持TableViewCell的可视化的。这使得我们可以用设置属性的方法来修改和维护表。

·生成继承UITableViewCell的类,就叫CustomCell吧

·生成一个Empty NIB,并在对象库中搜索TableView Cell,拖出来

·customclass当然是选择CustomCell

·在属性选择器中进行各种属性的设置:可重用标识符(用于controller管辖的可重用队列),Accessory为none,大小检查器看截图:

·把View放进去,然后就可以排版界面了。

注意:System Bold 17.0是黑体。。。

·注意delegate方法中是先调用的,它允许的行高具有决定性,所以一定要在这里保留最大的cell行高。

调试过程;

·程序启动不成功,说明缺少告知控件协议方法(实际上是显示多少行的方法)-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

return [self.computers
count];

}

添加以上方法,解决。

·程序启动成功,但是没有数据,于是想设置断点调试,发现程序无法进入返回cell的方法。

可推断是载入数据部分的代码出了问题,检查发现数据载入写在了ViewDidUnload,修正后就成功了。但是回想既然self.computers是空对象,应该在那里抛出一个空指针异常,但是程序没有报错。回去调试验证发现确实如此。后来再论坛上发现Objective-c可以向 nil
发送 message 而不会报错,我又纠结了。。。 这样做有利有弊,程序可能因此不用处理空指针,因为对象自己可以选择不去接收消息。但是调试的时候很麻烦,很有可能不知道错哪里。解决方法还是对nil进行条件验证。

四、UINavigationController表控件
这个对象的能力是以栈的后进先显示的方式进行controller 的显示,它以和标签栏不同的方式按照程序自己的流程控制了所有控制器的视图的向前显示。

作为开始,初始化此对象时就应该提供rootViewController,并向程序delegate注册,如下所示:



BIDFirstLevelController*first = [[BIDFirstLevelController
alloc]

initWithStyle:UITableViewStylePlain];

self.navController =[[UINavigationController
alloc]

initWithRootViewController:first];

[self.window
addSubview:navController.view];

[self.window
makeKeyAndVisible];

这样注册也是可以的:

MasterViewController*masterViewController = [[MasterViewController
alloc] initWithNibName:@"MasterViewController"
bundle:nil];

self.navigationController =[[UINavigationController
alloc] initWithRootViewController:masterViewController];

self.window.rootViewController =
self.navigationController;

[self.window
makeKeyAndVisible];

在tableViewController使用Navigation的能力:

[self.navigationController
pushViewController:childController

animated:YES];

4.2IndexPath

//new一个NSIndexPath

NSIndexPath*newPath = [NSIndexPath
indexPathForRow:rowinSection:0];

4.3 strong,retain,weak,assign自匹配宏

有时我们写个代码开源出来给别人用时,会被其他开发者抱怨编译不了,很多情况是版本的问题,尤其现在ARC的出现后关于weak,strong的问题让人头疼。有个开源代码这里做的很不错,就是MBProgressHUD
看下他是怎么做的:

#ifndefMB_STRONG


#if__has_feature(objc_arc)


#defineMB_STRONG strong


#else


#defineMB_STRONG retain


#endif


#endif

#ifndefMB_WEAK
#if __has_feature(objc_arc_weak)


#defineMB_WEAK weak


#elif__has_feature(objc_arc)


#defineMB_WEAK unsafe_unretained


#else
#defineMB_WEAK assign


#endif


#endif


非ARC的retain,相当于ARC的strong
iOS5的ARC中weak能在销毁时自动赋值nil,这是iOS4.x上使用ARC不具备,所以用的unsafe,非ARC自然是assign
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: