Iphone控件应用

 http://blog.csdn.net/dyhbrewer/article/details/6739711

已经入手iphone开发有3个礼拜了,之前做了个小应用就是把库里的所有控件集合到这个应用里面,通过这个应用,发现控件的使用基本都有固定的代码套用。现总结如下,如有错误,欢迎指正。


  UIButton

常用事件: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

示例:

// silder的值反映到sliderLabel

- (IBAction) sliderValueChanged: (id)sender

{

    UISlider *slider = (UISlider *)sender;

    int progressAsInt = (int)(slider.value + 0.5f);

    NSString *newText = [[NSString allocinitWithFormat:@"%d", progressAsInt];

    sliderLabel.text = newText;

    [newText release];


五:UISwitch(开关)

代码

// 属性on取开关的状是否on
//
 方法setOn置开关的状
- (IBAction) switchChanged: (id)sender
{
    UISwitch *whichSwitch = (UISwitch *)sender;
    BOOL setting = whichSwitch.on;
    [leftSwitch setOn:setting animated:YES];
    [rightSwitch setOn:setting animated:YES];
}



: UISegmentedControl

#definekSegmentIndex_Switches        0
#define kSegmentIndex_Button          1
- (IBAction) segmentChanged: (id)sender
{
   
 switch([sender selectedSegmentIndex])
    {
       
 casekSegmentIndex_Switches:
            leftSwitch.hidden = NO;
            rightSwitch.hidden = NO;
            doSomethingButton.hidden = YES;
           
 break;
       
 casekSegmentIndex_Button:
            leftSwitch.hidden = YES;
            rightSwitch.hidden = YES;
            doSomethingButton.hidden = NO;
           
 break;       

    }

}

 

七、UIActionSheet(操作表)和UIAlertView(警报)

UIActionSheet用于迫使用户在两个或更多选项之间进行选择都模式视图。操作表从屏幕底部弹出,显示一系列按钮供用户选择,用户只有单击了一个按钮后才能继续使用使用应用程序。

UIAlertView(警报)以蓝色圆角矩形都形式出现在屏幕的中部,警报可显示一个或多个按钮。

为了让控制器类充当操作表的委托,控制器类需要遵从UIActionSheetDelegate协议。我们通过在类声明都超类之后都尖括号中添加协议名称来实现。

 

@interface UntitledViewController : UIViewController
    <UIActionSheetDelegate>
{
   
 // ....
}

// 建操作表:
- (IBAction) buttonPressed: (id)sender
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                      initWithTitle:
@"Are you sure?"
                      
delegate:self
                      cancelButtonTitle:
@"Cancel" 
                      destructiveButtonTitle:
@"Yes,I'm sure." 
                      otherButtonTitles:nil];
   
    [actionSheet showInView:self.view];
    [actionSheet release];
}

// 实现方法:
#pragma mark ActionSheet Delegate Methods
- (
void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
   
   
 if(buttonIndex != [actionSheet cancelButtonIndex])
    {
        NSString *text = [[NSString alloc] initWithFormat:
@"test alert"];
        UIAlertView *alert = [[UIAlertView alloc]
                      initWithTitle:
@"Something was done." 
                      message:text
                     
 delegate:self
                      cancelButtonTitle:
@"OK!" 
                      otherButtonTitles:nil];
        [alert show];
        [alert release];
        [text release];
    }
}

//- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
//{
//    NSLog(@"%d",buttonIndex);
//}


 

.闭键盘

运行iPhone测试发现键盘不能自。下面来解决问题让输入完后按键盘上的return即可关,或者触摸背景关闭键盘(数字键盘上没有returniphone程序常设计为触摸背景关闭键盘输入状)。

 

完成入后关闭键盘
当用按下return键时,将生成一个Did End On Exit事件,此需要textField取消控件已关闭键盘
当前正在与之交互的控件称第一响(firstResponder)
方法 - (BOOL)resignFirstResponder 使触此操作的控件取消第一响者状

在控制器类头文件中声明方法:

- (IBAction) textFieldDoneEditing: (id)sender;

保存目后在IB中将textFieldDid End On Exit事件接到此方法。保存IB返回Xcode实现此方法:

- (IBAction) textFieldDoneEditing: (id)sender
{
   
 // 消第一响者状
    [sender resignFirstResponder];
}

 

重新编译并运行,发现可以通按下return来完成入了!

 
触摸背景关闭键盘
并非所有键盘布局都有return,例如数字键盘。苹果公司的iPhone程序是这样做的:在大多数有textField的情况下,在试图中任何无活控件的位置按下手指都可让键盘消失。实现此功能非常简单,只要建一个不可的按,将其置于其他所有元素的后面,用于通知textField检测到触摸操作生成第一响者状

// 触摸屏幕关闭键盘
- (IBAction) backgroundTap: (id)sender
{
   
 // 在非第一响者控件上resignFirstResponder绝对安全的。
    //
 因此可以放心的所有textFieldresignFirstResponder
    [textField resignFirstResponder];
}

保存代 来到 IB 。我 们现 在需要更改 nib 试图 的底 层类
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.  [prg_Bar setProgress:这边传入%];


先新增一个View-Based Application Project,并且命名为:ProgressView

然后,编辑ProgressViewViewController.h

1.  @interface ProgressViewViewController : UIViewController {

2.    IBOutlet UIProgressView *prg_Bar;

3.    NSInteger progress;

4.  }

5.  @property (nonatomic, retain) UIProgressView *prg_Bar;

6.  @property (nonatomic) NSInteger progress;

7.   

8.  @end


编辑ProgressViewViewController.m

1.  @implementation ProgressViewViewController

2.  @synthesize prg_Bar, progress;

3.   

4.  -(void) run{

5.    progress = progress > 10 ? 0 : progress + 1;

6.    [prg_Bar setProgress:progress*0.1];

7.  }

8.   

9.  - (void)viewDidLoad {

10.    [super viewDidLoad];

11.    progress = 0;

12.    [prg_Bar setProgress:progress*0.1];

13.    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(run) userInfo:nil repeats:YES];

14.}

十. 使用activity indicator视图

创建

1.  spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

2.  [spinner setCenter:CGPointMake(kScreenWidth/2.0, kScreenHeight/2.0)]; // I do this because I'm in landscape mode

3.  [self.view addSubview:spinner]; // spinner is not visible until started


开始:

1.  [spinner startAnimating];


停止:

1.  [spinner stopAnimating];

2.  spinner.hidden=YES;


十一. UITableView 

-、建立 UITableView

 DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];
 [DataTable setDelegate:self];
 [DataTable setDataSource:self];
 [self.view addSubview:DataTable];
 [DataTable release];


二、UITableViewMethod说明

//Section
总数
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
 return TitleData;
}


// Section Titles
//
每个section显示的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
 return @"";
}


//
指定有多少个分区(Section),默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
 return 4;
}


//
指定每个分区中有多少行,默认为1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
}


//
绘制Cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
  
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             SimpleTableIdentifier];
    if (cell == nil) {  
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier: SimpleTableIdentifier] autorelease];
 }
 cell.imageView.image=image;//
未选cell时的图片
 cell.imageView.highlightedImage=highlightImage;//
选中cell后的图片
 cell.text=//.....
 return cell;
}


//
行缩进
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
 NSUInteger row = [indexPath row];
 return row;
}


//
改变行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}


//
定位
[TopicsTable setContentOffset:CGPointMake(0, promiseNum * 44 + Chapter * 20)];


//
返回当前所选cell
NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];
[TopicsTable selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];


[tableView setSeparatorStyle:UITableViewCellSelectionStyleNone];


//
选中Cell响应事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

 [tableView deselectRowAtIndexPath:indexPath animated:YES];//
选中后的反显颜色即刻消失
}


//
判断选中的行(阻止选中第一行)
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];
    if (row == 0)
        return nil;
   
    return indexPath;
}


//
划动cell是否出现del按钮
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
}


//
编辑状态
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{



[topicsTable setContentSize:CGSizeMake(0,controller.promiseNum * 44)];
//
右侧添加一个索引表
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
}

//
返回Section标题内容
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
}

//
自定义划动时del按钮内容
- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath


//
跳到指的row or section
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];

 

三、在UITableViewCell上建立UILable多行显示

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
  UILabel *Datalabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 44)];
  [Datalabel setTag:100];
  Datalabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  [cell.contentView addSubview:Datalabel];
  [Datalabel release];
 
 UILabel *Datalabel = (UILabel *)[cell.contentView viewWithTag:100];
 [Datalabel setFont:[UIFont boldSystemFontOfSize:18]];
 Datalabel.text = [data.DataArray objectAtIndex:indexPath.row];
 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

 

 

//
选中cell时的颜色

typedef enum {
    UITableViewCellSelectionStyleNone,
    UITableViewCellSelectionStyleBlue,
    UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle

 

//cell
右边按钮格

typedef enum {
    UITableViewCellAccessoryNone,                   // don't show any accessory view
    UITableViewCellAccessoryDisclosureIndicator,    // regular chevron. doesn't track
    UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks
    UITableViewCellAccessoryCheckmark               // checkmark. doesn't track
} UITableViewCellAccessoryType

//
是否加换行线

typedef enum {
    UITableViewCellSeparatorStyleNone,
    UITableViewCellSeparatorStyleSingleLine
} UITableViewCellSeparatorStyle

//
改变换行线颜色
tableView.separatorColor = [UIColor blueColor];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值