当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - How to pass NSURL object from one viewcontroller to another viewcontroller

问题描述:

I have an NSURL object in ScanViewController class and pass it to the ListViewController class.

NSURL *url = [NSURL URLWithString:@"http:// some url"];

I am using xib's in my project and can't find an alternative for

[self.storyboard instantiateViewControllerWithIdentifier:]

[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)

{

if(error || !data)

{

NSLog(@"JSON NOT posted");

}

else

{

NSLog(@"JSON data posted!");

id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:Nil];

if([jsonObject respondsToSelector:@selector(objectForKey:)])

{

NSDictionary *dictionaryForUserID = [jsonObject valueForKey:@"ProjID"];

NSLog(@" Project Id = %@", dictionaryForUserID);

NSURL *urlToDisplayInListView = [NSURL URLWithString:[NSString stringWithFormat:@"http://some url/%@", dictionaryForUserID]]; **//Pass this object to other viewcontroller**

}

}

}];

网友答案:

I assume that you're performing a segue from one VC to another. If so, you can do this in your prepareForSegue:sender: method:

if ([segue.identifier isEqualToString:@"segueToListViewController"]) {
    [(ListViewController *)segue.destinationViewController setURL:[NSURL URLWithString:@"http:// some url"]];
}

You have to declare a property in your destination VC to handle the URL, and you will need an accessor method to set that property.

EDIT

As danypata suggested, if you're not using segues, try the following

ListViewController *listViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ListViewControllerIdentifier"];
[listViewController setURL:[NSURL URLWithString:@"http:// some url"]];
[self presentViewController:listViewController animated:YES completion:nil];
分享给朋友:
您可能感兴趣的文章:
随机阅读: