您的位置:首页 > 编程语言

iphone开发阶段总结(三)代码示例

2011-03-29 15:14 330 查看
8.通过UISegmentedControl 控制UIView(UIView *switch) 显示或是隐藏的状态;

- (IBAction)toggleShowHide:(id)sender
{
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
//查询uisegmentcontrol的值
NSInteger segment = segmentedControl.selectedSegmentIndex;

if (segment == 0)
[switchView setHidden:NO];
else
[switchView setHidden:YES];

}

9.UISwitch (UISwitch *soundSwitch) 开关的控制
-(IBAction)switchChanged:(id)sender
{
UISwitch *whichSwitch = (UISwitch *)sender;
BOOL setting = whichSwitch.isOn;
[SoundSwitch setOn:setting animated:YES];
}

10.操作表
使用操作表必须符合<UIActionSheetDelegate>协议(在.h头文件里的UIViewController后添加);
同时操作表是模式化的(模式化就是程序显示操作表时,程序的其他任何部分都是不可操作的,
必须在操作表上作出选择,才可以执行其他的操作);
-(IBAction)doSomething:(id)sender
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"ready?"
delegate:self
cancelButtonTitle:"No"
destructiveButtonTitle:"Yes"
otherButtonTitle:@"Woo",@"Cool",nil];
[actionSheet showInview:self.view];
[actionSheet release];
}
里面的delegate:self 通过将self做为委托参数传递给actionsheet:didDisMissWithButtonIndex:
actionSheet的方法,当按钮被按下的时候,委托的actionsheet:didDisMissWithButtonIndex:
actionSheet方法将会被调用;

通过UIActionSheetDelegate的actionSheet:didDismisswithButtonIndex方法的buttonIndex来指向
操作的索引;
①如果想通过操作表的destructiveButton按钮来执行某项操作;
if(buttonIndex == [actionSheet destructiveButtonIndex])
{
doSomething;//你要做的操作,显示警报 或是别的动作
}
②通过操作表的Woo按钮来执行某项操作;
if(buttonIndex == [actionSheet firstOtherButtonIndex])
{
doSomething;
}
③通过操作表的cool按钮来执行某项操作;
if(buttonIndex-1 == [actionSheet firstOtherButtonIndex])
{
doSomething;
}

11.警报

NSString *msg = nil;
msg = @"Please click on the go button to get the coupons.";

UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Congratulations"
message:msg
delegate:self
cancelButtonTitle:@"Back"
otherButtonTitles:@"Go",@"Got", nil];
[alert show];
[alert release];
[msg release];

对警报里的按钮做相对应的点击处理(以点击Got按钮显示店铺的宣传网站)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0:
// Cancel button
break;
case 1:
//Go !button
break;
default:
// Got! button
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:
@"http://www.strawberrycones.com/menu/"]];
}
}

12.自动旋转
UIInterfaceOrientationPortrait //直立
UIInterfaceOrientationPortraitUpsideDown //倒立
UIInterfaceOrientationLandscapeLeft //左翻转
UIInterfaceOrientationLandscapeRight //右翻转

如果想设定应用程序是否支持自动翻转,通过interfaceOrientation 来设定;(设定后也要手动的调整对应的页面视图的属性)
-(BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{
return(interfaceOrientation == 视图朝向);
}

13.切换视图
①.首先添加控制视图的viewController类(负责程序的视图跳转 此处定义为switchViewController);

②.添加好程序需要的视图控制类和相对应的xib文件,并在视图xib文件的类特性编辑器里设置好它的基类和视图
输出口;

③.在应用程序委托里写好调用视图跳转的函数
.h
+(Pizza_RockAppDelegate *)App;
.m
+(Pizza_RockAppDelegate *)App{
return (Pizza_RockAppDelegate *)[[UIApplication sharedApplication]delegate];
}

④.在控制视图的viewController类里,写跳转视图的方法;
-(void)showRockView
{
if(self.pizza_RockViewController == nil)
{
self.pizza_RockViewController = [[Pizza_RockViewController alloc]initWithNibName:@"Pizza_RockViewController" bundle:nil];
}
[self removeAllView];
[self.view insertSubView:self.pizza_RockViewController.view atIndex:0];
}

-(void)removeAllView
{
for(NSInteger i=0;i<[self.view.subViews count];i++)
{
[[self.view.subviews objectAtIndex:i] removeFromSuperview];
}
}

⑤.在视图控制类里调用应用程序委托里写好的函数来完成视图的跳转
-(IBAction)buttonClick:(id)sender
{
[[Pizza_RockAppDelegate App].switchViewController showRockView];//showRockView为在控制视图跳转的类里定义好的方法
[label removeFromSuperview];
}

14.建立工具栏(Tab Bar Controller)
①.创建控制视图的viewController类以及控制视图所对应的视图xib文件(工具栏有几个标签控制器,则创建几个控制视图类文件 );

②.设置视图nib对应的viewController类,并设置好File's Owner的输出口为对应的view;

③.在应用程序委托里创建UITabBarController的实例;
④.在MainWindow.xib里,添加一个Tab Bar Controller,并在属性检查器里添加工具栏所需要的标签控制器,并设置好标签控制器
对应的标题名称、NIB NAME、图标;

15.表视图
表视图从遵循UITableViewDelegate协议的对象中获取配置数据;
从遵循UITableViewDataSource协议的对象获得行数据;

①.查看指定分区并返回表分区中的行数;(NSArray *listData)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.listData count];
}

②.根据声明的字符串键值来绘制表视图中的行
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//声明表单元里的键值(标识符)
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
//根据这个声明的键值返回可重用的表视图标识符
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];

if (cell == nil)
{
cell =[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SimpleTableIdentifier]autorelease];
}

NSUInteger row = [indexPath row];
cell.text = [listData objectAtIndex:row];
return cell;
}

③.为指定的行添加图像
// 0 为要添加图像的行数的下标
if ([indexPath row] == 0)
{
UIImage *image = [UIImage imageNamed:@"beef.png"];
cell.image = image;
}

④.设置表视图的缩进级别(树状的表视图以后弄清楚)
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
//indexPath row
//10为缩进的级别
NSUInteger row = 10;
return row;

}

⑤.处理行的选择
-(void)tableView:(UITableView *)talbeView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
doSomething;
}

⑥.导入外部数据
NSDictionary *names;
NSArray *keys;

-(void)viewDidLoad
{
//读取name.plist的数据
NSString *path=[[NSBundle mainBundle] pathForResource:@"name" ofType:@"plist"];
NSLog(@"viewDidLoad/path = %@",path);
NSDictionary *dict =[[NSDictionary alloc] initWithContentsOfFile:path];
self.allNames = dict;
[dict release];
//根据names 排序并传递给array数组
NSArray *array = [[names allKeys] sortedArrayUsingSelector:@selector(compare:)];
Self.keys=array;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: