警告框(AlertView)时模态的,不关闭它就不能做其他事情,所以不是以下几种情况不应该随便使用。
1、应用不能继续运行。
如内存不足,没有网络。一般只需要一个按钮。
2、询问另一个解决方案。
不能运行时,询问是否可以用3G网络。
3、询问对操作的授权。
涉及到访问隐私信息的时候,需要用户授权,如位置、相册等。
操作表(ActionSheet)可以给用户提供多个选择。可以利用它将某个图片发给新浪微博或者Facebook平台。
/ 实现UIAlertViewDelegate
// 这个委托其实没有用到,就当练练手,因为警告窗口有两个按钮索引
// No为0,Yes为1
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"buttonIndex = %li", (long)buttonIndex);
}
// 实现UIActionSheetDelegate
// 这个委托也没有实际意义,就是在输出命令窗口输出按下的索引数,以实现响应
- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"buttonIndex = %li", (long)buttonIndex);
}
- (IBAction)testAlertView:(id)sender {
// 警告框在上文已叙述
// delegate 参数用于设置该警告窗口的委托对象
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert"
message: @"Alert text goes here"
delegate:self cancelButtonTitle:@"No"
otherButtonTitles:@"Yes",
nil];
[alertView show];
}
- (IBAction)testActionSheet:(id)sender {
// cancelButtonTitle 设置取消标题
// destructiveButtonTile 设置破坏型按钮,只能有一个在最上面
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:@"取消"destructiveButtonTitle:@"破坏性按钮"
otherButtonTitles:@"新浪微博", nil];
// 设置为自动样式
actionSheet.actionSheetStyle = UIActionSheetStyleAutomatic;
[actionSheet showInView:self.view];
}