application:didFinishLaunchingWithOptions 在 viewDidLoad 之前加载
所以我正在编写一个可以读取 JSON feed 的应用程序。在我的应用程序: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 必须连接到文件的所有者。我的已经连接上了。
我将不胜感激任何帮助!
谢谢大家。
So I am writing an app that will read a JSON feed. In my application:didFinishLaunchingWithOptions, I am writing some code to download the JSON string and store it into a local NSString variable. I will then pass that string into ListingsViewController (which is the Root VC of the NavigationController). When I print out the JSON data in ListingsViewController, it is showing me (null) which is making me think that viewDidLoad is loading before - which seems illogical?
So here is my application: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;
}
My viewDidLoad method is as follows:
- (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];
}
Please note that a common solution is to make sure that the App Delegate in MainWindow.xib must be connected to the File's Owner. Mine already is connected.
I will appreciate any help!
Thank you everyone.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,您正在使用代码设置视图控制器,但它永远不会显示在导航控制器中。您只需设置一个视图控制器,为其 jsonData 分配一个字符串并立即销毁该视图控制器。我非常确定您获得的输出来自您在主 XIB 中创建的不同视图控制器。
您想要做的是在 XIB 中创建一个空的导航控制器,然后执行以下操作:
这将实际显示您创建的视图控制器。
另请记住,当您部署应用程序时,您需要异步加载 json 数据并处理网络错误(苹果将在各种网络条件下测试您的应用程序)
well you are setting up a view controller with your code, but it is never shown within the navigation controller. you just set up a view controller, assign its jsonData a string and destroy the view controller immediately. I#m pretty sure the output you get is from a different view controller that you create in your main XIB.
what you want to do is create an empty navigation controller in your XIB and then do the following:
this will actually display the view controller you created.
Also remember that when you deploy your application, you need to load your json-data asynchronously and handle network errors (apple will test your app under various network conditions)