http://blog.csdn.net/dyhbrewer/article/details/6739711
已经入手iphone开发有3个礼拜了,之前做了个小应用就是把库里的所有控件集合到这个应用里面,通过这个应用,发现控件的使用基本都有固定的代码套用。现总结如下,如有错误,欢迎指正。
常用事件:Touch Up Inside
二、UITextField
常用属性:Text:要显示的文本。
Placeholder:指定将要在文本字段中以灰色显示的占位符文本。
Clear When Editing Begins:用户触摸此字段时是否删除字段中的值。
Text Input Traits:文本输入特征。
三、UIImageView
常用属性:
image:指定图像文件
Mode:图像在视图内部的对齐方式以及是否缩放图像以适应视图。选择任何图像缩放的选项都会潜在地增加处理开销,因此最好避开这些选项,并在导入图像之前调整好图像大小。通常Mode属性为Center。
Alpha:图像透明度。一般设置为1.0
Background:该属性继承自UIView,但它不会影响图像视图的外观,请忽略此属性。
Drawing复选框:选中Opaque表示视图后面的任何内容都不应该绘制,并且允许iPhone都绘图方法通过一些优化来加速绘图。
Clear Context Before Drawing:选中它之后,iPhone将使用透明黑色绘制控件覆盖都所有区域,然后才实际绘制控件。考虑到性能问题,并且适用情况很少,通常很少需要选中ClearContext Before Drawing。
Interaction复选框:
User Interaction Enabled:指定用户能否对此对象进行操作。
Multiple Touch:是否能够接收多点触摸事件。
四:UISlider(滑块)
常用属性:Value Changed
示例:
//
- (IBAction) sliderValueChanged: (id)sender
{
UISlider
int
NSString
sliderLabel.text
[newText
}
五:UISwitch(开关)
代码
// 属性on:获取开关的状态是否为on
// 方法setOn:设置开关的状态
- (IBAction) switchChanged: (id)sender
{
}
六: UISegmentedControl
#definekSegmentIndex_Switches
#define kSegmentIndex_Button
- (IBAction) segmentChanged: (id)sender
{
}
七、UIActionSheet(操作表)和UIAlertView(警报)
UIActionSheet用于迫使用户在两个或更多选项之间进行选择都模式视图。操作表从屏幕底部弹出,显示一系列按钮供用户选择,用户只有单击了一个按钮后才能继续使用使用应用程序。
UIAlertView(警报)以蓝色圆角矩形都形式出现在屏幕的中部,警报可显示一个或多个按钮。
为了让控制器类充当操作表的委托,控制器类需要遵从UIActionSheetDelegate协议。我们通过在类声明都超类之后都尖括号中添加协议名称来实现。
@interface UntitledViewController : UIViewController
{
}
// 创建操作表:
- (IBAction) buttonPressed: (id)sender
{
}
// 实现方法:
#pragma mark ActionSheet Delegate Methods
- (void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonInde
{
}
//- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
//{
//
//}
八.关闭键盘
运行iPhone模拟器测试后发现键盘不能自动关闭。下面来解决这个问题,让输入完后按键盘上的return即可关闭,或者触摸背景关闭键盘(数字键盘上没有return键,iphone程序常设计为触摸背景关闭键盘输入状态)。
完成输入后关闭键盘
当用户按下return键时,将生成一个Did End On Exit事件,此时我们需要让textField取消控件已关闭键盘。
用户当前正在与之交互的控件称为第一响应者(firstResponder)。
方法 - (BOOL)resignFirstResponder 使触发此操作的控件取消第一响应者状态。
我们在控制器类头文件中声明方法:
- (IBAction) textFieldDoneEditing: (id)sender;
保存项目后在IB中将textField的Did End On Exit事件连接到此方法。保存IB返回Xcode实现此方法:
- (IBAction) textFieldDoneEditing: (id)sender
{
}
重新编译并运行,发现可以通过按下return键来完成输入了!
通过触摸背景关闭键盘
并非所有键盘布局都有return键,例如数字键盘。苹果公司的iPhone程序是这样做的:在大多数有textField的情况下,在试图中任何无活动控件的位置按下手指都可让键盘消失。实现此功能非常简单,只要创建一个不可见的按钮,将其置于其他所有元素的后面,用于通知textField在检测到触摸操作时生成第一响应者状态。
// 通过触摸屏幕关闭键盘
- (IBAction) backgroundTap: (id)sender
{
}
在 IB 主窗口中 单击选择 View , command+4 调 出身份 检查 器。将 标 有 Class 的字段由 UIView 更改 为 UIControl 。能 够 触 发 操作方法的所有控件都是 UIControl 的子 类 ,所以通 过 更改底 层类 ,此 View 将能 够 触 发 操作方法。
现 在按 command + 2 应该 能看到 Event 。将 Touch Down 事件 连 接到 File's Owner 的 backgroundTap: 操作。
保存 IB 返回 Xcode 后 编译 并运行,可以看到触摸 视图 中没有活 动 控件的任何位置 ( 屏幕背景或 lable 等控件 ) 都将触 发 backgroundTap: 操作。
九. UIProgressView(Progress bar)
界面是用InterfaceBuilder拉的,所以写得简洁
这个范例会让程序每秒增加10%的长度
对ProgressBar传入0~1之间的小数,使用方式是:
1.
先新增一个View-Based Application Project,并且命名为:ProgressView
然后,编辑ProgressViewViewControll
1.
2.
3.
4.
5.
6.
7.
8.
编辑ProgressViewViewControll
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.}
十. 使用activity indicator视图
创建
1.
2.
3.
开始:
1.
停止:
1.
2.
十一. UITableView
-、建立 UITableView
二、UITableView各Method说明
//Section总数
- (NSArray *)sectionIndexTitlesForTab
}
// Section Titles
//每个section显示的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
}
//指定有多少个分区(Section),默认为1
- (NSInteger)numberOfSectionsInTableV
}
//指定每个分区中有多少行,默认为1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
}
//绘制Cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
}
//行缩进
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAt
}
//改变行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
}
//定位
[TopicsTable setContentOffset:CGPointMake(0, promiseNum * 44 + Chapter * 20)];
//返回当前所选cell
NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];
[TopicsTable selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositio
[tableView setSeparatorStyle:UITableViewCellSelection
//选中Cell响应事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
//判断选中的行(阻止选中第一行)
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath
{
}
//划动cell是否出现del按钮
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
}
//编辑状态
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingSt
forRowAtIndexPath:(NSIndexPath *)indexPath
{
}
[topicsTable setContentSize:CGSizeMake(0,controller.promiseNum * 44)];
//右侧添加一个索引表
- (NSArray *)sectionIndexTitlesForTab
}
//返回Section标题内容
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
}
//自定义划动时del按钮内容
- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmati
//跳到指的row or section
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositio
三、在UITableViewCell上建立UILable多行显示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
}
//选中cell时的颜色
typedef enum {
} UITableViewCellSelection
//cell右边按钮格
typedef enum {
} UITableViewCellAccessory
//是否加换行线
typedef enum {
} UITableViewCellSeparator
//改变换行线颜色
tableView.separatorColor = [UIColor blueColor];