警告框和操作表(IOS开发)

时间:2022-02-24 23:10:20

警告框(AlertView)时模态的,不关闭它就不能做其它事情,所以不是下面几种情况不应该随便使用。

1、应用不能继续执行。

如内存不足,没有网络。一般仅仅须要一个button。

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