我想顯示一個簡單的加載對話框,當我的應用程序發生某些事情時。我想我會創建一個新的視圖,爲其添加一個標籤,然後將該視圖設置爲當前視圖的子視圖。iPhone:添加「加載」子視圖
當這樣做時,我什麼都看不到!
這裏是我怎麼寫我的方法:
- (void)showLoading {
UIView *loading = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
loading.backgroundColor = [UIColor blackColor];
UILabel *txt = [[UILabel alloc] initWithFrame:CGRectMake(198, 9, 94, 27)];
txt.text = @"Loading...";
txt.textColor = [UIColor whiteColor];
[loading addSubview:txt];
[super.view addSubview:loading];
[super.view bringSubviewToFront:loading];
[loading release];
[txt release];
}
我這樣做完全錯了嗎?
編輯: 我把它添加到viewDidLoad方法,它的作品我多麼希望:
loading = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
loading.backgroundColor = [UIColor blackColor];
UILabel *txt = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 94, 27)];
txt.text = @"Loading...";
txt.textColor = [UIColor whiteColor];
[loading addSubview:txt];
[txt release];
[self.view addSubview:loading];
[self.view bringSubviewToFront:loading];
但是從方法加載的時候,它似乎滯後,並沒有顯示出來了一點。
我想在顯示HUD時顯示HUD,但由於某種原因,它會一直等待循環顯示HUD。這是正常的嗎? – 2010-08-16 02:37:17
這是一個正常的錯誤:試圖從主線程或Web線程以外的線程獲取Web鎖。這可能是從輔助線程調用UIKit的結果。現在崩潰...? – 2010-08-16 02:56:43
通過在主線程中運行我的UI代碼修復了錯誤。 – 2010-08-16 20:55:37