iOS - 提醒



提醒的用法

提醒用于向用户提供重要信息。只有在警报视图中选择选项后,我们才能使用该应用继续进行后续操作。

重要属性

  • alertViewStyle
  • cancelButtonIndex
  • 委托
  • 消息
  • 按钮数
  • 标题

重要方法

- (NSInteger)addButtonWithTitle:(NSString *)title
- (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex
- (void)dismissWithClickedButtonIndex:
   (NSInteger)buttonIndex animated:(BOOL)animated
- (id)initWithTitle:(NSString *)title message:
   (NSString *)message delegate:(id)delegate
   cancelButtonTitle:(NSString *)cancelButtonTitle
   otherButtonTitles:(NSString*)otherButtonTitles, ...
- (void)show

更新 ViewController.h 如下所示 -

使您的类符合警报视图委托协议,方法是将 < UIAlertViewDelegate> 添加到 ViewController.h 中,如下所示。

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIAlertViewDelegate> {
}
@end

添加自定义方法 addAlertView

-(void)addAlertView {
   UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:
   @"Title" message:@"This is a test alert" delegate:self 
   cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
   [alertView show];
}

实现警报视图委托方法

#pragma mark - Alert view delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:
   (NSInteger)buttonIndex {
      switch (buttonIndex) {
         case 0:
            NSLog(@"Cancel button clicked");
         break;
        
         case 1:
            NSLog(@"OK button clicked");
         break;
        
         default:
         break;
      }
   }
}

更新 ViewController.m 中的 viewDidLoad 如下所示 -

(void)viewDidLoad {
   [super viewDidLoad];
   [self addAlertView];
}

输出

当我们运行应用程序时,我们将得到以下输出 -

iOS Tutorial
ios_ui_elements.htm
广告
© . All rights reserved.