iPhone开发常用控件UIActionSheet和UIAlertView的学习是本文要介绍的内容,主要来学习iphone开发中的控件如何来使用,来看本文详细内容。
一、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 alloc] initWithFormat:@"%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
- //
- // 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
- //
- // 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
- //
- // 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
小结:iPhone开发常用控件:UIActionSheet和UIAlertView学习的内容介绍完了,希望通过本文的学习能对你有所帮助!