標籤:指標 變數 ram 可變 inter nim ace view nil
此文學習來源為:http://study.163.com/course/introduction/1002858003.htm
此工程檔案實現功能:
1、警告對話方塊和等待提示器的概念
2、警告對話方塊和等待提示器的屬性
3、警告對話方塊和等待提示器的使用
===========================ViewController.h指令碼==============================
@interface ViewController : UIViewController <UIAlertViewDelegate>
{
//定義一個警告對話方塊視圖對象
UIAlertView* _alertView;
//等待提示對象
//當下載或載入比較大的檔案時,可以顯示此控制項,處於提示等待狀態
UIActivityIndicatorView* _activityIndicatorView;
}
@property (retain,nonatomic) UIAlertView* alertView;
@property (retain,nonatomic) UIActivityIndicatorView * activityIndicatorView;
@end
===========================ViewController.m指令碼==============================
@interface ViewController ()
@end
@implementation ViewController
//屬性和成員變數的同步
@synthesize alertView = _alertView;
@synthesize activityIndicatorView = _activityIndicatorView;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
for (int i = 0; i < 2; i++) {
UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(100, 100 + 100 * i, 100, 40);
if (i == 0) {
[btn setTitle:@"警告對話方塊" forState:UIControlStateNormal];
}
else if (i == 1)
{
[btn setTitle:@"等待指標" forState:UIControlStateNormal];
}
btn.tag = 101 + i;
[btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
}
- (void) pressBtn:(UIButton*) btn
{
//警告對話方塊
if (btn.tag == 101) {
//建立警告對話方塊
//P1:對話方塊標題
//P2:提示資訊
//P3:處理按鈕事件的代理對象
//P4:取消按鈕的文字
//P5:其他按鈕文字
//P6....:添加其他按鈕
//PLast:表示按鈕添加結束
//兩個按鈕橫著排,多個豎著排
_alertView = [[UIAlertView alloc] initWithTitle:@"警告"
message:@"你的手機電量過低,即將關機,請儲存好資料"
delegate:self
cancelButtonTitle:@"取消" //取消按鈕永遠放最後
otherButtonTitles:@"OK",@"11",@"22", nil];
//顯示對話方塊
[_alertView show];
}
//建立等待提示器
else if (btn.tag == 102)
{
//寬度和高度不可變更
_activityIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(100, 300, 80, 80)];
//設定提示的風格:小灰,小白,大白
_activityIndicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;//UIActivityIndicatorViewStyleWhite;//UIActivityIndicatorViewStyleGray;
self.view.backgroundColor = [UIColor blackColor];
[self.view addSubview:_activityIndicatorView];
//啟動動畫並顯示
[_activityIndicatorView startAnimating];
//停止等待動畫並隱藏
//[_activityIndicatorView stopAnimating];
}
}
//當點擊對話方塊的按鈕時,調用此函數
//P1:對話方塊對象本身
//P2:按鈕的索引
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"index = %ld\n",buttonIndex);
}
//對話方塊即將消失,此函數被調用
- (void) alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(@"即將消失");
}
//對話方塊已經消失,此函數被調用
- (void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(@"對話方塊已經消失");
}
程式運行結果:
按鈕
警告對話方塊(2個按鈕:橫排)
警告對話方塊(4個按鈕:豎排)
等待指標:(大白風格)
學習總結:
- 重點:警告對話方塊和等待提示器的概念
- 痛點:警告對話方塊和等待提示器的用法
源碼連結地址:https://pan.baidu.com/s/1yrOLXZZeu9MiOWtMq5-EGA 密碼:7t1l
IOS開發之XCode學習014:警告對話方塊和等待提示器