博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

LINK ADDRESS:http://www.cnblogs.com/elfsundae/archive/2010/11/11/1874486.html

iPhone起步-2:iPhone常用控件、UIActionSheet、UIAlertView

一、UILabel

 

二、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

 

#define kSegmentIndex_Switches 0
#define kSegmentIndex_Button 1


- (IBAction) segmentChanged: (id)sender
{
switch ([sender selectedSegmentIndex])
{
case kSegmentIndex_Switches:
leftSwitch.hidden
= NO;
rightSwitch.hidden
= NO;
doSomethingButton.hidden
= YES;
break;
case kSegmentIndex_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);
//}

示例:

视图有一个UISegmentedControl,"Switches"下有两个UISwitch

"Button"下有一个“Do Something"的UIButton

触摸"Do Something"Button时弹出UIActionSheet

触摸选择"Yes,I'm sure."时弹出 UIAlertView

OBJECTIVE-C CODE   :UntitledViewController.h
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//
//  UntitledViewController.h
//  Untitled
//
//  Created by Elf Sundae on 11/10/10.
//  Copyright 2010 www.cnBlogs.com/ElfSundae. All rights reserved.
//

#import <UIKit/UIKit.h>

#define kSegmentIndex_Switches		0
#define kSegmentIndex_Button		    1


@interface UntitledViewController : UIViewController
	<UIActionSheetDelegate>
{
	UISwitch	* leftSwitch;
	UISwitch	* rightSwitch;
	UIButton	* doSomethingButton;
}

@property (retain, nonatomic) IBOutlet UISwitch *leftSwitch;
@property (retain, nonatomic) IBOutlet UISwitch *rightSwitch;
@property (retain, nonatomic) IBOutlet UIButton *doSomethingButton;

- (IBAction) switchChanged: (id)sender;
- (IBAction) segmentChanged: (id)sender;
- (IBAction) buttonPressed: (id)sender;

@end
OBJECTIVE-C CODE   :UntitledViewController.m
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
//  UntitledViewController.m
//  Untitled
//
//  Created by Elf Sundae on 11/10/10.
//  Copyright 2010 www.cnBlogs.com/ElfSundae. All rights reserved.
//

#import "UntitledViewController.h"

@implementation UntitledViewController

@synthesize leftSwitch;
@synthesize rightSwitch;
@synthesize doSomethingButton;

// 属性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];
}


- (IBAction) segmentChanged: (id)sender
{
switch ([sender selectedSegmentIndex])
{
case kSegmentIndex_Switches: leftSwitch.hidden = NO;
rightSwitch.hidden = NO;
doSomethingButton.hidden = YES;
break;
case kSegmentIndex_Button: leftSwitch.hidden = YES;
rightSwitch.hidden = YES;
doSomethingButton.hidden = NO;
break;

}


}


- (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];
}

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } - (void)viewDidUnload {
// Release any retained subviews of the main view. // e.g. self.myOutlet = nil; self.leftSwitch = nil;
self.rightSwitch = nil;
self.doSomethingButton = nil;
[super viewDidUnload];
}


- (void)dealloc {
[leftSwitch release];
[rightSwitch release];
[doSomethingButton release];
[super dealloc];
}


#pragma mark - #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); //} @end