天天看點

iOS開發之八:UISlider、UISegmentedControl、UIPageControl的使用

本文的三種控件,用的也非常多,而我也是經常圖懶,而去打開原來的項目去拷貝,現在記錄一下,就不用去項目中去找這些控件的用法了。

一、UIActivityIndicatorView 的使用

UIActivityIndicatorView 俗稱“風火輪”,也有人稱之為菊花,安裝黑蘋果系統時,遠景論壇上都稱之為菊花。

iOS開發之八:UISlider、UISegmentedControl、UIPageControl的使用

 它主要是用來告訴使用者目前正在加載資料,讓使用者等待一下。長這個樣子的:

iOS開發之八:UISlider、UISegmentedControl、UIPageControl的使用

它的常用屬性和方法也比較少:

// 設定風格
@property(nonatomic) UIActivityIndicatorViewStyle
activityIndicatorViewStyle;
// 停止時,隐藏視圖,預設為YES
@property(nonatomic) BOOL hidesWhenStopped;
// 修改顔色,注意版本問題
@property (readwrite, nonatomic, retain) UIColor *color
// 開始動畫
- (void)startAnimating;
// 停止動畫
- (void)stopAnimating;
// 判斷動畫的狀态(停止或開始)
- (BOOL)isAnimating;
           

使用UIActivityIndicatorView  的示例代碼如下:

UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityView.center = CGPointMake(160, 200);
[activityView startAnimating];
activityView.hidesWhenStopped = NO;
[self.window addSubview:activityView];
[activityView release];
    
[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(test:) userInfo:activityView repeats:NO];
    
 //狀态欄也顯示風火輪
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
           

把定時器的方法也記錄一下吧,設定的是3秒之後隐藏“風火輪”。

- (void)test:(NSTimer *)timer
{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    
    UIActivityIndicatorView *activityView = [timer userInfo];
    [activityView stopAnimating];
           

注意:蘋果自帶的“風火輪”效果有時候不能滿足我們的需要,我們可以用第三方的架構 MBProgressHUD,它有多種效果,可以附帶圖檔,或者附帶文字,還可以改裝成安卓裡的toast。至于MBProgressHUD的使用,我就不介紹了,給個傳送門:MBProgressHUD的使用,這是别人寫的。裡面還有github的下載下傳位址。

推薦一個demo網站:code4app,裡面也有MBProgressHUD的使用demo----》demo資料

二、UIAlertView的使用

UIAlertView 是用來提示使用者,供使用者選擇的,我印象裡,它是會阻塞線程的。而且視窗的級别非常高。 直接上代碼好了:

//UIAlertView 初始化
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"标題" message:@"提示文本資訊" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alertView show];
[alertView release];
           

上面的delegate參數設定時,需要實作UIAlertViewDelegate中的方法:

#pragma mark - AlertView Delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"buttonIndex : %d", buttonIndex);
}
           

我們可以根據buttonIndex來區分使用者點選的是哪一按鈕,來執行不同的操作。

三、UIActionSheet的使用 這裡需要注意的是,actionSheet showInView這個方法,将其添加的位置不對,會造成有時候點選沒有反應的情況。

UIActionSheet *actionSheet = [[[UIActionSheet alloc] initWithTitle:@"title" delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:@"destructive" otherButtonTitles:@"other1", @"other2", @"other3", @"other3", nil] autorelease];
[actionSheet showInView:self.window];// 這裡我經常這樣寫:[actionSheet showInView:[UIApplication shareApplication].keyWindow];
[actionSheet release];
           

同理如果你需要根據不同按鈕觸發不能的操作的話,也是要實作其delegate方法。

#pragma mark - ActionSheet Delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"clickedButtonAtIndex : %d", buttonIndex);
}
           

我們可以根據buttonIndex來區分使用者點選的是哪一按鈕,來執行不同的操作。

繼續閱讀