IOS复习.docx
- 文档编号:9658741
- 上传时间:2023-02-05
- 格式:DOCX
- 页数:27
- 大小:27.17KB
IOS复习.docx
《IOS复习.docx》由会员分享,可在线阅读,更多相关《IOS复习.docx(27页珍藏版)》请在冰豆网上搜索。
IOS复习
一、基本控件
1.文本框(TextField)
委托:
UITextFieldDelegate
实现隐藏键盘:
(1)实现UITextFieldDelegate
(2)重写
(BOOL)textFieldShouldReturn:
(UITextField*)textField
//使其失去第一响应者
[textFieldresignFirstResponder];
(3).将委托指向给viewcontroller:
self.textfield.delegate=self;
2.文本域(TextView)
委托:
UITextViewDelegate
实现隐藏键盘:
(1).实现UITextViewDelegate
(2).重写(BOOL)textView:
(UITextView*)textViewshouldChangeTextInRange:
(NSRange)rangereplacementText:
(NSString*)text
//使其失去第一响应者
[textViewresignFirstResponder];
(3).将委托指向给viewcontroller
3.开关(UISwitch)
方法:
isOn:
获取开关当前状BOOLisopen=myswitch.isOn;
setOn:
设置开关状态[self.leftswitchsetOn:
isopen];
4.分段(UISegmentedControl)
方法:
selectedSegmentIndex:
获取选中的索引,从0开始
5.滑块(UISlider)
使用:
设置最大值,最小值,起始值
方法:
获得滑块的当前值intmyvalue=self.myslider.value;
6.进度条(UIProgressView)
-(void)download{//定义一个操作进度条的方法
//累加进度条值
self.myproview.progress=self.myproview.progress+0.1f;
if(self.myproview.progress>=1.0){
[self.mytimerinvalidate];//停止定时器
//将按钮的标题改为重新下载,同时将进度条归零
[self.btndownsetTitle:
@"重新下载"forState:
NO];
self.myproview.progress=0.0f;
//弹出对话框提示成功
UIAlertView*alert=[[UIAlertViewalloc]initWithTitle:
@"下载提示"message:
@"下载完成"delegate:
selfcancelButtonTitle:
@"确定"otherButtonTitles:
nil,nil];
[alertshow];
}
}
7.定时器(NSTimer)
if(self.mytimer!
=nil&&self.mytimer.isValid){//暂停,要么是继续
NSString*btntilte=self.btndown.titleLabel.text;
if([btntilteisEqualToString:
@"暂停"]){
//暂停定时器
[self.mytimersetFireDate:
[NSDatedistantFuture]];
//设置按钮值
[self.btndownsetTitle:
@"继续"forState:
NO];
}else{
//继续定时器
[self.mytimersetFireDate:
[NSDatedate]];//继续
[self.btndownsetTitle:
@"暂停"forState:
NO];
}
}else{
//定义并启动定时器
self.mytimer=[NSTimerscheduledTimerWithTimeInterval:
1.0target:
selfselector:
@selector(download)userInfo:
nilrepeats:
YES];
[self.btndownsetTitle:
@"暂停"forState:
NO];
}
8.状态指示器(UIActivityIndicatorView)
方法:
1.获得指示器当前的状态BOOLstate=self.myactivity.isAnimating;
2.设置指示器停止或开始stopAnimating;startAnimating;
9.警告框(UIAlertView)
协议:
UIAlertViewDelegate
UIAlertView*alert=[[UIAlertViewalloc]initWithTitle:
@"提示框"//对话框标题
message:
@"这是一个警告框"//弹出信息
delegate:
self//委托
cancelButtonTitle:
@"取消"//取消按钮的标题
otherButtonTitles:
@"确定",//其他按钮的标题nil];
[alertshow];//显示对话框
10.Web视图(UIWebView)
NSURL *url=[NSURLURLWithString:
@".hk"];
NSURLRequest *request=[[NSURLRequestalloc] initWithURL:
url];
[myWebView loadRequest:
request];
11.动作表单(UIActionSheet)
协议:
UIActionSheetDelegate
UIActionSheet*sheet=[[UIActionSheetalloc]initWithTitle:
nildelegate:
self
cancelButtonTitle:
@"取消分享"
destructiveButtonTitle:
@"分享到QQ空间"o
therButtonTitles:
@"分享到新浪微博",@"twitter",@"facebook",nil];
[sheetshowInView:
self.view];
//获取被选中菜单的索引
-(void)alertView:
(UIAlertView*)alertViewclickedButtonAtIndex:
(NSInteger)buttonIndex{
NSLog(@"%d",buttonIndex);
}
12.日期选择器(UIDatePicker)
NSDate*mydate=self.mydatepicker.date;//获取datepicker控件时间值
NSDateFormatter*dfm=[[NSDateFormatteralloc]init];
[dfmsetDateFormat:
@"YYYY-MM-ddHH:
mm:
ss"];//格式时间
NSString*datestr=[dfmstringFromDate:
mydate];
13.自定义选择器(UIPickerView)
协议:
数据源UIPickerViewDataSource,委托UIPickerViewDelegate
使用:
1.定义接受数据源的变量
NSDictionary*mydata//全部数据;
NSArray*provinces;//省数据
NSArray*city;//市数据
2.接受数据源
NSBundle*bundle=[NSBundlemainBundle];//从plist文件中读取所有数据
NSString*pathList=[bundlepathForResource:
@"xxx"ofType:
@"plist"];//获得文件名
self.mydata=[[NSDictionaryalloc]initWithContentsOfFile:
pathList];//读数据
self.provinces=[self.mydataallKeys];//获得省
self.city=[self.mydataobjectForKey:
provinceName];//市
//默认初始值去第一个省的所有市
NSString*provinceName=[self.provincesobjectAtIndex:
0];//1.获得省名称
self.city=[self.mydataobjectForKey:
provinceName];
//绑定数据源及委托
self.myPickerView.dataSource=self;
self.myPickerView.delegate=self;
3.//返回某一个拨轮中有多少个值
-(NSInteger)pickerView:
(UIPickerView*)pickerViewnumberOfRowsInComponent:
(NSInteger)component{
if(component==0){
return[self.provincescount];
}else{
return[self.citycount];
}
}
4.//返回当前picker共有几个拨轮
-(NSInteger)numberOfComponentsInPickerView:
(UIPickerView*)pickerView{
return2;
}
5.//显示某一个拨轮中row标题
-(NSString*)pickerView:
(UIPickerView*)pickerViewtitleForRow:
(NSInteger)rowforComponent:
(NSInteger)component{
if(component==0){
return[self.pDataobjectAtIndex:
row];
}else{
return[self.cDataobjectAtIndex:
row];
}
}
6.//选中的拨轮行
-(void)pickerView:
(UIPickerView*)pickerViewdidSelectRow:
(NSInteger)rowinComponent:
(NSInteger)component{
if(component==0){
NSString*p=[self.pDataobjectAtIndex:
row];
NSArray*arr=[self.plistDataobjectForKey:
p];
self.cData=arr;
[self.myPickerViewreloadComponent:
1];
}
}
二、表视图
一.表视图分类
1.形式上分类
1.1动态表视图(重点)
1.2静态表视图(孤岛式视图)
分析:
静态表视图一般用于设计和规划页面
动态表视图常用于数据显示
2.从控件分类
2.1UITableView(基本视图控件)
2.2UITableViewController(基本视图控件+标准类)
注意:
如果使用2.1则,需要显示实现委托(UITableViewDelegate)以及数据源(UITableviewDataSource)
如果使用2.2则,需要生成一个集成UITableViewController的类,同时将视图中的Table指向该类
3.从功能分类
3.1简单表视图(只有一个节)
3.2复杂表视图(多个节以及节标题,节索引等等)
3.3自定义CELL表视图
二.表视图结构
1.表视图基本结构
1.1节1.2节标题1.3表标题1.4表尾1.5单元格(Cell)/行
注意:
1.一个表视图至少有一个节2.一个节有多个Cell
2.单元格结构/行
1.图标(image)2.正文区(正文标题,正文,正文尾)3.扩展区
三.表视图中委托以及数据源
需要重写的方法:
1).选中某一行的操作,详解表格复选框(checked)
-(void)tableView:
(UITableView*)tableViewdidSelectRowAtIndexPath:
(NSIndexPath*)indexPath;
2).某一单元格编辑样式,详见可编辑表视图
-(UITableViewCellEditingStyle)tableView:
(UITableView*)tableVieweditingStyleForRowAtIndexPath:
(NSIndexPath*)indexPath;
删除样式:
UITableViewCellEditingStyleDelete
添加样式:
UITableViewCellEditingStyleInsert
3).当前表视图有多少节
-(NSInteger)numberOfSections;
4).当前节有多少行
-(NSInteger)numberOfRowsInSection:
(NSInteger)section;
5).添加节
-(void)insertSections:
(NSIndexSet*)sectionswithRowAnimation:
(UITableViewRowAnimation)animation;
6).删除节
-(void)deleteSections:
(NSIndexSet*)sectionswithRowAnimation:
(UITableViewRowAnimation)animation;
7).添加行,详解可编辑表视图
-(void)insertRowsAtIndexPaths:
(NSArray*)indexPathswithRowAnimation:
(UITableViewRowAnimation)animation;
8).删除行,详见可编辑表视图
-(void)deleteRowsAtIndexPaths:
(NSArray*)indexPathswithRowAnimation:
(UITableViewRowAnimation)animation;
9).设置表视图可编辑,详见可编辑表视图
-(void)setEditing:
(BOOL)editinganimated:
(BOOL)animated;
10).返回所有Cell
-(UITableViewCell*)tableView:
(UITableView*)tableViewcellForRowAtIndexPath:
(NSIndexPath*)indexPath;
11).当前节返回多少行
-(NSInteger)tableView:
(UITableView*)tableViewnumberOfRowsInSection:
(NSInteger)section;
12).返回索引列,详见有索引的表视图
-(NSArray*)sectionIndexTitlesForTableView:
(UITableView*)tableView;
规格化字符串:
[[NSStringalloc]initWithFormat:
@"%d",myvalue];
设置控件的隐藏显示:
self.leftswitch.hidden=NO;
设置button按钮的显示:
[self.btndownsetTitle:
@"暂停"forState:
NO];
视图控制器与导航
视图控制器分类
•UIViewController(自定义视图控制器的导航)
•UINaviagtionController(导航控制器)
•UITabBarController(标签控制器)
•UIPageViewController(电⼦子书导航风格)
•UISplitViewController(分屏导航--ipad)
•UIPopoverController(⽓气泡风格)
导航模式
•平铺导航模式(照片产品展示)
•标签导航模式(多配合树形使用,分类别数据显示)
•树形结构导航模式(常用作数据显示)
•模态视图(特殊)(常见用作登录注册页面之间的转换)
模态:
主要⽅方法
(显示模态视图)presentViewController:
animated:
completion
(关闭模态视图)dismissViewControllerAnimated:
completion
模态样式分类
UIModalTransitionStyleCoverVertical(从底向 上模式)
UIModalTransitionStyleFlipHorizontal.(从右往左水平翻转)
UIModalTransitionStyleCrossDissolve.(淡入淡出)
UIModalTransitionStylePartialCurl.(卷起一个边角翻页)
模态视图参数回传(广播模式)开发步骤
1.定义NSDictionary(广播数据封装)
2.定义⼴广播对象
[[NSNotificationCenter default]postNotificationName:
Object:
userInfo];
3.定义接受⼴广播⽅方法
4.定义⼴广播监听
2.广播和模态视图
(1)-(void)viewDidLoad
{
[superviewDidLoad];
//添加广播监听
[[NSNotificationCenterdefaultCenter]addObserver:
selfselector:
@selector(getNotification:
)name:
@"registerok"object:
nil];
}
//定义接受广播的方法
-(void)getNotification:
(NSNotification*)notification{
NSDictionary*mydata=[notificationuserInfo];
NSString*uname=[mydataobjectForKey:
@"uname"];
NSLog(@"用户注册的用户名为:
%@",uname);
}
-(IBAction)onClickRegister:
(id)sender{
//获得故事模板对象
UIStoryboard*mainStoryboard=[UIStoryboardstoryboardWithName:
@"Main"bundle:
nil];
//通过故事模板对象获得控制视图
UIViewController*registerViewController=[mainStoryboardinstantiateViewControllerWithIdentifier:
@"registerViewController"];
//设计模态显示样式
self.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
//开始调用
[selfpresentViewController:
registerViewControlleranimated:
YEScompletion:
^{
NSLog(@"开始调用下一个视图");
}];
}
(2点击注册OK,后将本视图中的填写的用户名回传给上一个视图,采取广播设计模式
-(IBAction)onClickOK:
(id)sender{
[selfdismissViewControllerAnimated:
YEScompletion:
^{
NSLog(@"关闭视图的同时将数据回传");
//定义广播数据,其类型为NSDisdictionary
NSDictionary*dictData=[NSDictionarydictionaryWithObject:
self.uNameText.textforKey:
@"uname"];
//定义广播
[[NSNotificationCenterdefaultCenter]postNotificationName:
@"registerok"//广播名称object:
niluserInfo:
dictData];
}];
}
-(IBAction)onClickCancel:
(id)sender{
//取消模态视图
[selfdismissViewControllerAnimated:
YEScompletion:
^{
NSLog(@"取消模态视图!
");
}];
}
平铺
1.UIPageViewController实现平铺
协议:
UIPageViewControllerDataSource
步骤:
1)拖拽PageViewController,修改StoryboardID,设置TransitionStyle为Scroll
2)拖拽新的viewController,修改StoryboardID,添加imageView,指定新的类
3)代码实在太多自己看去吧:
代码文件名pageViewControllerDemo
2.ScrollView实现平铺
协议:
UIScrollViewDelegate
步骤:
1)拖拽ScrollView及PageControl,生成IBOutlet
2)拖拽出多个ViewController,修改StoryboardID,添加imageView
3)在Scroll的类中定义多个UIView以对应多个ViewController
4)代码实现:
-(void)viewDidLoad
{[superviewDidLoad];
//初始化各个参数,首先初始化滚屏的宽度和高度,注意宽度设置为3个屏幕的宽度self.scrollView.contentSize=CGSizeMake(self.view.frame.size.width*3,self.view.frame.size.height);
self.scrollView.frame=self.view.frame;
//获得故事模板对象
UIStoryboard*mainStoryboard=[UIStoryboardstoryboardWithName:
@"Main"bundle:
nil];
//获得第一个视图控制对象
UIViewController*page1ViewController=[mainStoryboardinstantiateViewControllerWithIdentifier:
@"page1"];
self.page1=page1Vie
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- IOS 复习