![](/img/trans.png)
[英]application : didFinishLaunchingWithOptions and viewDidLoad
[英]application:didFinishLaunchingWithOptions loading before viewDidLoad
所以我正在編寫一個應用程序來讀取 JSON 提要。 在我的應用程序:didFinishLaunchingWithOptions 中,我正在編寫一些代碼來下載 JSON 字符串並將其存儲到本地 NSString 變量中。 然后我將該字符串傳遞給 ListingsViewController(它是 NavigationController 的根 VC)。 當我在 ListingsViewController 中打印出 JSON 數據時,它向我顯示 (null),這讓我認為 viewDidLoad 之前正在加載 - 這似乎不合邏輯?
所以這是我的應用程序:didFinishLaunchingWithOptions
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Grab the feeds
NSURL *jsonURL = [NSURL URLWithString:@"http://www.shoofeetv.com/iphonexml/view/all_channels.json"];
NSString *jsonData = [[NSString alloc] initWithContentsOfURL:jsonURL];
// Pass jsonData to the ListingsViewController
ListingsViewController *listingsViewController = [[ListingsViewController alloc] initWithNibName:@"ListingsViewController" bundle:nil];
listingsViewController.jsonData = jsonData;
[listingsViewController release];
// Display the navigation controller
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
我的 viewDidLoad 方法如下:
- (void)viewDidLoad {
self.navigationItem.title = @"Listings";
UIBarButtonItem *checkinButton = [[UIBarButtonItem alloc]
initWithTitle:@"Check In"
style:UIBarButtonItemStylePlain
target:self
action:@selector(switchView)];
self.navigationItem.rightBarButtonItem = checkinButton;
[checkinButton release];
NSLog(@"%@", self.jsonData);
[super viewDidLoad];
}
請注意,一個常見的解決方案是確保 MainWindow.xib 中的 App Delegate 必須連接到文件的所有者。 我的已經連接了。
我將不勝感激任何幫助!
謝謝大家。
好吧,您正在使用您的代碼設置視圖 controller,但它從未在導航 controller 中顯示。 您只需設置一個視圖 controller,為其 jsonData 分配一個字符串並立即銷毀視圖 controller。 我很確定您得到的 output 來自您在主 XIB 中創建的不同視圖 controller。
您要做的是在您的 XIB 中創建一個空導航 controller ,然后執行以下操作:
// Pass jsonData to the ListingsViewController
ListingsViewController *listingsViewController = [[ListingsViewController alloc] initWithNibName:@"ListingsViewController" bundle:nil];
listingsViewController.jsonData = jsonData;
[self.navigationController pushViewController:listingsViewController animated:NO];
[listingsViewController release];
// Display the navigation controller
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
這實際上將顯示您創建的視圖 controller。
還要記住,當你部署你的應用程序時,你需要異步加載你的 json-data 並處理網絡錯誤(蘋果會在各種網絡條件下測試你的應用程序)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.