今天练习的内容是:
使用UIAlertView生成警告
利用系统来播放声音警告;
练习步骤:
1. 创建view_base_application, 项目名称为GettingAttention
2. 打开GettingAlertViewController.h
声明UILabel statusMessage
声明:
-(IBAction)doAlert:(id)sender;
-(IBAction)doActionSheet:(id)sender;
-(IBAction)doSound:(id)sender;
-(IBAction)doAlertSound:(id)sender;
-(IBAction)doVibrate:(id)sender;
3. 负责函数和File's Owner连接
安装具体的UI, 按住control,连线到File's Owner, 选择正确的处理函数,
4。
4. 1完成doAlert:(id)sender
主要思路就是初始化uialertView, uialerView show, uialertView release
4.2 doActionSheet:(id)sender
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"doActionSheet"message:@"doActionSheet"delegate:selfcancelButtonTitle:@"ok"otherButtonTitles:@"Maybe later",@"Never",nil];
[alert show];
[alert release];
基本步骤和相同,区别在与otherButtonTitles:@"",@"",nil
4.3 doSound:(id)sender;
首先导入import AudioToolBox.frame框架
然后import <AudioToolbox/AudioToolbox.h>
SystemSoundID soundId;
NSString *soundFlie = [[NSBundle mainBundle]pathForResource:@"alertsound" ofType:@"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:soundFlie], &soundId);
AudioServicesPlayAlertSound(soundId);
4.4 doVibrate:(id)sender的方法
这个比较简单,直接调用方法AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
整个实例相对简单,主要复杂点在于如何调用AudioToolbox框架来播放系统声音
详细代码如下
//
// GettingAlertViewController.h
// GettingAlert
//
// Created by 旭 陈 on 11-12-4.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
@interface GettingAlertViewController : UIViewController<UIAlertViewDelegate> {
IBOutlet UILabel *statusMessage;
}
@property(nonatomic,retain)IBOutlet UILabel *statusMessage;
-(IBAction)doAlert:(id)sender;
-(IBAction)doActionSheet:(id)sender;
-(IBAction)doSound:(id)sender;
-(IBAction)doAlertSound:(id)sender;
-(IBAction)doVibrate:(id)sender;
@end
//
// GettingAlertViewController.m
// GettingAlert
//
// Created by 旭 陈 on 11-12-4.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "GettingAlertViewController.h"
@implementation GettingAlertViewController
@synthesize statusMessage;
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (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;
}
- (void)dealloc {
[super dealloc];
}
-(IBAction)doAlert:(id)sender{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
-(IBAction)doActionSheet:(id)sender{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"doActionSheet" message:@"doActionSheet" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:@"Maybe later",@"Never",nil];
[alert show];
[alert release];
}
-(IBAction)doSound:(id)sender{
SystemSoundID soundId;
NSString *soundFlie = [[NSBundle mainBundle]pathForResource:@"alertsound" ofType:@"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:soundFlie], &soundId);
AudioServicesPlayAlertSound(soundId);
}
-(IBAction)doAlertSound:(id)sender{
SystemSoundID soundId;
NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"soundeffect" ofType:@"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:soundFile], &soundId);
AudioServicesPlayAlertSound(soundId);
}
-(IBAction)doVibrate:(id)sender{
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if ([title isEqualToString:@"Maybe later"]) {
statusMessage.text = @"Maybe Later";
}else if ([title isEqualToString:@"Never"]) {
statusMessage.text = @"Never";
}
}
@end
声明:
1。上述例子代码来源于书籍<<Iphone开发入门经典>>,经过本人的实际练习通过。
2。 需要本例子代码的童鞋,请留下电子邮件。