(十)弹出框Alert与ActionSheet

第一种方式:中间弹窗

从中间弹出的窗口称为AlertView。

可以设置多个按钮,取消按钮会放在对右端或者最下端,按钮超过两个,会竖着排列。

UIAlertView *alert = [[[UIAlertView alloc] init] initWithTitle:@"标题" message:@"消息" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"A",@"B",@"C", nil];
[alert show];

Tip:一定不要忘记show方法。

如果要监听按钮的点击,要通过代理。先继承protocol <UIAlertViewDelegate>

然后调用alertView方法:buttonIndex是从0开始的,0是取消。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"%d",buttonIndex);
}
注意代理方法传入了控件本身,道理在于:为了告知是哪个框触发了代理(可能有多个对象,但是他们触发的是同一个代理,因此需要判断代理的触发者)。
Tip:继承自UIView的控件都可以通过设置tag成员变量来修改标签,也可以通过点语法直接判断tag来认出控件。


第二种方式:底部向上Action Sheet

一般是危险操作会这样弹窗。

 UIActionSheet *sheet = [[[UIActionSheet alloc] init] initWithTitle:@"标题" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"毁灭性操作" otherButtonTitles:@"其他操作", nil];
 [sheet showInView:self.view];
需要注意的是cancelButton在最底部分离开,destructiveButton是危险操作,放到这里,其他按钮放到otherButton里面。

效果为:



原文地址:https://www.cnblogs.com/aiwz/p/6154243.html