IOS提示框大概有两种方法,一个是UIAlertView另一个是UIActionsheet。
UIAlertView:
1 2 3 4 5 6 7 8 9 10 11 12
| func showAlertView(){ var alert = UIAlertView() alert.title = "Title" alert.message = "message" alert.delegate = self alert.addButtonWithTitle("canel") alert.addButtonWithTitle("OK") alert.cancelButtonIndex = 0 alert.show() }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| - (void)showAlerView{ UIAlertView *alert = [[UIAlertView alloc] init]; alert.title = @"Title"; alert.message = @"message"; alert.delegate = self; [alert addButtonWithTitle:@"cancel"]; [alert addButtonWithTitle:@"OK"]; alert.cancelButtonIndex = 0; [alert show]; }
|
设置delegate需要在class后面加UIActionSheetDelegate
1 2
| class ViewController: UIViewController ,UIAlertViewDelegate
|
1 2
| @interface ViewController : UIViewController<UIAlertViewDelegate>
|
然后通过这个函数检测用户点击了哪个button
1 2 3 4 5 6 7 8 9 10
|
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) { }
|
1 2 3 4 5 6 7 8 9
|
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ NSLog(@"%ld",(long)buttonIndex); }
|
UIActionSheet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| func showActionSheet(){ var actionSheet = UIActionSheet() actionSheet.delegate = self actionSheet.title = "Title" actionSheet.addButtonWithTitle("cancel") actionSheet.addButtonWithTitle("Action1") actionSheet.addButtonWithTitle("destructiveButton") actionSheet.destructiveButtonIndex = 2 actionSheet.cancelButtonIndex = 0 actionSheet.showInView(barButton, animated: true) }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| - (void)showActionSheet{ UIActionSheet *actionSheet = [[UIActionSheet alloc] init]; actionSheet.delegate = self; actionSheet.title = @"Title"; [actionSheet addButtonWithTitle:@"cancel"]; [actionSheet addButtonWithTitle:@"Action1"]; [actionSheet addButtonWithTitle:@"destructiveButton"]; actionSheet.destructiveButtonIndex = 2; actionSheet.cancelButtonIndex = 0; [actionSheet showInView:self.view]; }
|
上面那个Title
是通过这行代码加上去的,实在恶心……
1 2
| actionSheet.title = "Title"
|
一般我不用……仔细一想我好像从来没用过~
设置delegate需要在class后面加UIActionSheetDelegate
1 2
| class ViewController: UIViewController ,UIActionSheetDelegate
|
1 2
| @interface ViewController : UIViewController<UIActionSheetDelegate>
|
然后通过这个函数检测用户点击了哪个button
1 2 3 4 5 6 7 8
|
func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) { }
|
1 2 3 4 5 6 7 8 9
|
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ }
|
不过IOS8之后苹果又新出了个叫做UIAlertController的东西统一了UIAlertView和UIActionSheet。
写到现在终于写了一篇我自己稍微知道点的东西了😂,想写点东西怎么就那么难……