2009-11-15 77 views
0

我無法顯示指標視圖。如何顯示指標

ItemController.h

#import <UIKit/UIKit.h> 


@interface ItemController : UITableViewController { 
    UIView* loadingView; 
    UIActivityIndicatorView* indicator; 
} 

@property (nonatomic, retain) UIView *loadingView; 
@property (nonatomic, retain) UIActivityIndicatorView *indicator; 

@end 

ItemController.m

..... 
- (void)netAccessStart { 


    loadingView = [[UIView alloc] initWithFrame:[[self view] bounds]]; 
    [loadingView setBackgroundColor:[UIColor blackColor]]; 
    [loadingView setAlpha:0.5]; 
    indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
    [[self view] addSubview:loadingView]; 
    [loadingView addSubview:indicator]; 
    [indicator setFrame:CGRectMake ((320/2)-20, (480/2)-60, 40, 40)]; 
    [indicator startAnimating]; 

} 

- (void)netAccessEnd { 


    [indicator stopAnimating]; 
    [loadingView removeFromSuperview]; 

} 

- (void)dealloc { 
    [loadingView release]; 
    [indicator release]; 
    [super dealloc]; 
} 
..... 

繼承類

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 
    [self netAccessStart]; 
    sleep(1); 
    [self netAccessEnd]; 
} 

回答

1

sleep()塊執行,這意味着你的應用程序被凍結在-viewWillAppear呼叫,在其中你的loadingView從它的sup刪除erview。換句話說,在[self netAccessStart];[self netAccessEnd];之間沒有繪圖。我假設你調用一個立即另一個用於測試後的目的,在這種情況下,我的-netAccessStart命令移到-viewDidAppear:並用以下替換sleep/-netAccessEnd電話:

[self performSelector:@selector(netAccessEnd) withObject:nil afterDelay:1]; 

...這將具有相同的效果,但不會阻止執行。

0

這是因爲你使用sleep,而不是真的在做事情。據推測,你的意思是從互聯網上取回一些東西;做到這一點,並且只有在連接完成或失敗時才發送自己的消息netAccessEnd

與延遲的另一種形式的仍然使用硬編碼的間隔(例如,1秒)的更換sleep是一個好主意。如果時間少於1秒,則會讓用戶等待的時間超過必要的時間。如果花費時間超過1秒,那麼在實際完成用戶等待的任何操作之前,您都需要關閉指示器。

你需要採取的指標下降時,無論用戶正在等待完成,不早,不晚。