这篇文章主要学习alertview 和 actionsheet这两个控件的使用。Action Sheet是从底部弹出,上面有2个或者2个以上的选项供用户选择,Alert就是一个警告框,上面有1个或者1个以上的按钮供用户进行选择。
(说明:其实这两个不是控件,而是ios 中的两个类,这里暂且这么叫吧。这2个类定义了2种不同类型的用于和用户交互的弹出框)
首先,使用这两个类要使用到其代理,UIAlertViewDelegate 和 UIAlertViewDelegate 。
下面,借用别的博客上的一段话来稍稍阐述一下delegate。
ios中有很多已经定义好的类可以供我们在编写程序时直接使用,例如UIActionSheet、UIAlertView等,这些类定义了很多method,我们可以调用这些method且不必知道这些method是如何实现的。但是有一个问题,如果我们想改变这些method的实现,那我们该这么做呢?一种方法是继承,我们可以继承一个类,然后在自己的类中重新写method,这是一个方法,但不是一个很方便的方法,有时候你仅仅需要改变很小的一个功能,却要继承一个很大的类,貌似有点复杂了,而且如果你需要一些不同的实现,那你就需要定义好多不同的类,这会很麻烦。为了使开发过程更加的方便,ios使用了另一种方法来达到同样的目的,就是使用delegate,我们使用一个已定义的类,然后使用委托\代理来改写类中的method,程序在运行时,delegate发现你创建了某个类的实例且改写了其中的method,这样程序在运行时就不会去调用原有的实现(当然你也可以调用原有的实现),而是直接调用你写的新的实现,从而达到自定义程序方法的目的。
首先介绍actionsheet。
1、只有一个OK按键
- (void)dialogSimpleAction { // open a dialog with just an OK button UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"UIActionSheet <title>" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"OK" otherButtonTitles:nil]; actionSheet.actionSheetStyle = UIActionSheetStyleDefault; [actionSheet showInView:self.view]; // show from our table view (pops up in the middle of the table) }
上面code中的最后一行:
[actionSheet showInView:self.view]
作用是显示actionSheet,每一个ActionSheet都需要有一个parent view,在parent view中显示自己,因为我们是单一视图项目(Single View),也只有一个View,因此这里的self.view就是说在actionSheet实现的这个view里显示。
UIActionSheetStyle Specifies the style of an action sheet. typedef enum { UIActionSheetStyleAutomatic = -1, UIActionSheetStyleDefault = UIBarStyleDefault, UIActionSheetStyleBlackTranslucent = UIBarStyleBlackTranslucent, UIActionSheetStyleBlackOpaque = UIBarStyleBlackOpaque, } UIActionSheetStyle;
- (void)dialogOKCancelAction { // open a dialog with an OK and cancel button UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"UIActionSheet <title>" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"OK" otherButtonTitles:nil]; actionSheet.actionSheetStyle = UIActionSheetStyleDefault; [actionSheet showInView:self.view]; // show from our table view (pops up in the middle of the table) }
- (void)dialogOtherAction { // open a dialog with two custom buttons UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"UIActionSheet <title>" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Button1", @"Button2", nil]; actionSheet.actionSheetStyle = UIActionSheetStyleDefault; actionSheet.destructiveButtonIndex = 1; // make the second button red (destructive) [actionSheet showInView:self.view]; // show from our table view (pops up in the middle of the table) }注意其中的 actionSheet.destructiveButtonIndex = 1;// make the second button red (destructive)
重点,这里要实现前面说到的UIActionSheetDelegate ,这里要实现一个方法。
#pragma mark - UIActionSheetDelegate - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { // the user clicked one of the OK/Cancel buttons if (buttonIndex == 0) { NSLog(@"ok"); } else { NSLog(@"cancel"); } }其中的buttonIndex就是分别对应哪一个按键,注意是从0开始的。
接着介绍alert。
1、只有一个OK按键
- (void)alertSimpleAction { // open an alert with just an OK button UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView" message:@"<Alert message>" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; }
- (void)alertOKCancelAction { // open a alert with an OK and cancel button UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView" message:@"<Alert message>" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; [alert show]; }
- (void)alertOtherAction { // open an alert with two custom buttons UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView" message:@"<Alert message>" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Button1", @"Button2", nil]; [alert show]; }
- (void)alertSecureTextAction { // open an alert with two custom buttons UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView" message:@"Enter a password:" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; alert.alertViewStyle = UIAlertViewStyleSecureTextInput; //An alert that allows the user to enter text. The text field is obscured(遮蔽) [alert show]; }
其中的alertViewStyle含有这几种类型。
UIAlertViewStyle
The presentation style of the alert.
typedef enum {
UIAlertViewStyleDefault = 0,
UIAlertViewStyleSecureTextInput,
UIAlertViewStylePlainTextInput,
UIAlertViewStyleLoginAndPasswordInput
} UIAlertViewStyle;
// create a temporary alert to test it's feature availability
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:nil
message:nil
delegate:self
cancelButtonTitle:nil
otherButtonTitles: nil] autorelease];
// only add this part to the table if secure alerts are available
if ([alert respondsToSelector:@selector(alertViewStyle)])
{
}
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
// use "buttonIndex" to decide your action
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { // use "buttonIndex" to decide your action UITextField *passWord = [actionSheet textFieldAtIndex:0]; if (buttonIndex == 1) { NSLog(@"password :%@",passWord.text); } }