加載我的UIViewController時,我基本上把一個微調器在頁面中間,直到內容加載,然後再回到主線程添加子視圖:異步UIView內容沒有顯示當回到主線程
- (void)viewDidLoad {
[super viewDidLoad];
UIActivityIndicatorView *aiv_loading = ... // etc
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Load content
NSString *s_checkout = [[BRNetwork sharedNetwork] getCheckoutInstructionsForLocation:self.locBooking.location];
UIView *v_invoice_content = [[BRNetwork sharedNetwork] invoiceViewForBooking:locBooking.objectId];
// Display the content
dispatch_sync(dispatch_get_main_queue(), ^{
if (s_checkout && v_invoice_content) {
[aiv_loading removeFromSuperview];
[self showContentWithText:s_checkout AndInvoice:v_invoice_content];
} else {
NSLog(@"No data received!"); // is thankfully not called
}
});
});
}
- (void) showContentWithText:(NSString *)s_checkout AndInvoice:(UIView *)v_invoice {
[self.view addSubview:[self checkoutTextWithText:s_checkout]]; // Most of the time displayed text
[self.view addSubview:[self completeCheckout]]; // always Displayed UIButton
[self.view addSubview:[self divider]]; // always displayed UIImageView
// Summary title
UILabel *l_summary = [[UILabel alloc] initWithFrame:CGRectMake(0, [self divider].frame.origin.y + 6 + 10, self.view.bounds.size.width, 20)];
l_summary.text = NSLocalizedString(@"Summary", nil);
[self.view addSubview:l_summary];
CGRect totalRect = CGRectMake([self divider].frame.origin.x, [self divider].frame.origin.y + 6 + 30, self.view.bounds.size.width - [self divider].frame.origin.x, 90);
_v_invoice = v_invoice;
_v_invoice.frame = totalRect;
[self.view addSubview:[self v_invoiceWithData:v_invoice]]; // THIS Pretty much never displayed
UITextView *l_invoice = [[UITextView alloc] initWithFrame:CGRectMake(0, _v_invoice.frame.origin.y + _v_invoice.frame.size.height + offset, 320.0, 50)];
l_invoice.text = NSLocalizedString(@"summary_emailed", nil);
[self.view addSubview:l_invoice]; // Always displayed
}
然而,並非顯示的所有內容。發票永遠不會在那裏,但會在幾分鐘後顯示出來。另一個異步創建的字符串s_content有時不顯示。
這似乎與內容創建隨機。最終結果非常簡潔,但對於生產版本來說不可靠。
我用未記錄的[self.view遞歸描述]來檢查一切是否存在,即使我沒有看到它們,它們都與那裏似乎是正確的幀。
任何指針? - layoutSubviews沒有幫助! - 在發票視圖中放置背景顏色顯示背景顏色
在'viewDidLoad'你的觀點應該已經被加載這樣就可以顯示你想要有和任何特別是如果你不希望每次顯示您的視圖的時間來重新顯示的東西,你應該** **使用'viewDidLoad' 。你在哪看到這個建議? –
剛剛嘗試,但不幸的是沒有改變任何東西!真正難以理解的是,當檢查self.view,右邊框,右邊文本的子視圖時,內容就在那裏,但沒有任何顯示。 –
@Ivan Genchev在'viewDidLoad'中存在視圖框架不準確的情況。 ,layoutSubviews尚未被調用。此外,還有一些UIView動畫由iOS執行,同時呈現可能縮短放置在'viewDidLoad'中的演示代碼的視圖。我同意,只有一次性代碼應該在其他地方,或者包含在像if(isFirstAppearance)這樣的檢查中,因爲只要視圖回到焦點時就執行viewDidAppear。 – Rufel