I've been trying to use this repo's sample project on experimenting as passing data between two different view controllers :https://github.com/RajendrasinhParmar/DrawerMenu
I've downloaded the demo code from GitHub and I've been trying to parse this demo JSON with following method:
{
"menu": [
{
"name": "Facebook",
"url": "http://www.facebook.com"
},
{
"name": "Twitter",
"url": "http://www.twitter.com"
}
]
}
#pragma mark - http stuff
- (void)getMenuList {
NSError *error;
NSString *url_string = [NSString stringWithFormat: @"http://json-schema-faker.js.org/#gist/6a6cc18dc58dca786194f390c0af28c9"];
NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString:url_string]];
NSMutableDictionary *response = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSMutableArray *menuArray = response[@"menu"];
// NSLog(@"response: %@", response);
for (int i=0; i < menuArray.count; i++) {
NSLog(@"%@", menuArray[i][@"name"]);
NSLog(@"%@", menuArray[i][@"url"]);
}
}
And display "name" in the table view at MenuViewController and pass the url to web view at ViewController when a cell selected.
My problem is. How can i assign name property in Tableview and pass url to webview when selected? I did some searching yet I've failed.
First, create an array property in the interface of your menuViewController and add the table view delegate and data source to your interface declaration. You will also need to add a table view to your menuViewController in interface builder and hook it up to your .h file.
@interface menuViewController:UIViewController <UITableViewDelegate, UITableViewDataSource>
@property(nonatomic) NSArray *menuArray;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
Next, after getting the JSON serialization, assign to your array defined in interface, then reload your table view
NSMutableDictionary *response = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
self.menuArray=response[@"menu"]; //Assign to array
[self.tableView reloadData];
Now, to setup the table, first assign the datasource and delegate in your viewDidLoad method
- (void)viewDidLoad {
self.tableView.dataSource=self;
self.tableView.delegate=self;
}
Now, implement the table view data source methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//Want as many cells as in array
return self.menuArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"menuCell"];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"defaultCell"];
}
//Display name of current item in array
cell.textLabel.text=[[self.menuArray objectAtIndex:indexPath.row]objectForKey:@"name"];
return cell;
}
Now, you should have your table fully set up with the names displaying. Now to pass url's to next controller, you need to implement another method.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Create new controller
ViewController *vc=[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
//Pass it the url
vc.url=[[self.menuArray objectAtIndex:indexPath.row]objectForKey:@"url"];
//Present it
[self presentViewController:vc animated:YES completion:nil];
}