call NSDictionary in current view controller - iphone

i have an function in which i am calling values from dictionary that is defined in other view controller. I have to call this method in view didload method of current view controller.
here is my code:
-(void)setData:(NSDictionary *)dic {
self.jsonLabel.text = [dic objectForKey:#"Description"];
self.jsonImage.image = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: [dic objectForKey:#"PictureURL"]]]];}

Try this
NSURL *jsonURL = [NSURL URLWithString:[jsonItem objectForKey:#"PictureURL"]];
Update:
See you allocate the dictionary while the second View controller inside its alloc init method.
Then you can pass the dictionary from the first view controller as
secondViewController.dict = self.dict;
Then you push the second view controller. Check the value of the dictionary in viewDidLoad: Then you can proceed if the dictionary is available.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//make sure jscontroller is allocated before this point.
NSDictionary *tempDict = [[NSDictionary alloc]init];
// set tempDict here with value or object aBook.PictureURL.
jscontroller.jsonItem = tempDict;
[tempDict release];
NSLog(#"json ndic.......%#", jscontroller.jsonItem);
[self.navigationController pushViewController:jscontroller animated:YES];
}

If viewControllerObject is the necessary object of the view controller that defines this function then allocate the object and call like this
- (void)viewDidLoad {
[viewControllerObject setData:urDict];
}

I was using this code in otherviewcontroller
and trying to retrieve in currentViewController
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
JSONViewController *controller = [[JSONViewController alloc] initWithNibName:#"JSONView" bundle:nil];
EmpDictionary=[[NSMutableDictionary alloc]init];
[EmpDictionary setValue:aBook.Description forKey:#"Description"];
[EmpDictionary setValue:aBook.PictureURL forKey:#"PictureURL"];
[EmpDictionary setValue:aBook.WeatherID forKey:#"WeatherID"];
EmpArray=[[NSMutableArray alloc]init];
[EmpArray addObject:EmpDictionary];
NSDictionary *itemAtIndex = [self.EmpArray objectAtIndex:indexPath.row];
[self.navigationController pushViewController:controller animated:YES];
[controller setData:itemAtIndex];}

Related

How to send data to a detail vc from callOutAccessoryControlTapped: method

I'm using the didSelectRowAtIndexPath: to pass some data from a table vc to a detail vc using the code below that works fine.
What I'm trying to do is to also send this data from a map callOutAccessoryControlTapped: method but I am unsure how to send the data that way.
How would I send
detailViewController.descriptionTextViewString = [[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"short_description"];
using the
callOutAccessoryControlTapped: method?
The objectAtIndex:indexPath.row is unrecognized from within the map callOutAccessoryControlTapped: method.
Here's my didSelectRowAtIndexPath: code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
ScrollView_ExampleViewController *detailViewController = [[ScrollView_ExampleViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
detailViewController.latStr = [[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"address"] objectForKey:#"lat"];
detailViewController.lngStr = [[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"address"] objectForKey:#"lng"];
detailViewController.addressStr = [[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"address"] objectForKey:#"address"];
detailViewController.titleStr = [[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"title"];
detailViewController.mainImageUrl = [[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"image"];
detailViewController.listingId = [[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"id"];
detailViewController.descriptionTextViewString = [[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"short_description"];
[self.navigationController pushViewController:detailViewController animated:YES];
}
Here's my map annotation calloutAccessoryControlTapped: method
- (void)mapView:(MKMapView *)mv annotationView:(MKAnnotationView *)pin calloutAccessoryControlTapped:(UIControl *)control {
ScrollView_ExampleViewController *detailViewController = [[ScrollView_ExampleViewController alloc] initWithNibName:#"ScrollView_ExampleViewController" bundle:nil];
MyAnnotation *theAnnotation = (MyAnnotation *) pin.annotation;
detailViewController.titleStr = theAnnotation.title;
detailViewController.addressStr = theAnnotation.subtitle;
// detailViewController.latStr = theAnnotation.latString;
// detailViewController.lngStr = theAnnotation.lngString;
// detailViewController.url = theAnnotation.theUrl;
[self.navigationController pushViewController:detailViewController animated:YES];
}
You can use the following method to get the tableView's selected index:
NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];

UITable view to detail view JSON parsing with AFNetworking

I managed to parse data from YouTube API on UITable view using AFNetworking but i cant figure out how to use
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
How do i pass the JSON data to the next view ?
Here is the link to the file: google drive
Here is my code
#import "KKViewController.h"
#import "AFNetworking.h"
#import "AFJSONRequestOperation.h"
#interface KKViewController ()
#end
#implementation KKViewController
#synthesize movies = _movies, count = _count;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.movies = [[NSArray alloc] init];
NSURL *url = [[NSURL alloc] initWithString:#"http://gdata.youtube.com/feeds/api/playlists/PL0l3xlkh7UnvLdr0Zz3XZZuP2tENy_qaP?v=2&alt=jsonc&max-results=50"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
self.movies = [JSON valueForKeyPath:#"data.items.video"];
// NSLog(##, video)
//
self.count = [JSON valueForKeyPath:#"data.items.video.thumbnail"];
[self.activityIndicatorView stopAnimating];
[self.tableView setHidden:NO];
[self.tableView reloadData];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(#"Request Failed with Error: %#, %#", error, error.userInfo);
}];
[operation start];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (self.movies && self.movies.count) {
return self.movies.count;
} else {
return 0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = #"Cell Identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
}
NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];
NSDictionary *countt = [self.count objectAtIndex:indexPath.row];
cell.textLabel.text = [movie objectForKey:#"title"];
cell.detailTextLabel.text = [movie objectForKey:#"uploader"];
NSURL *url = [[NSURL alloc] initWithString:[countt objectForKey:#"hqDefault"]];
[cell.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:#"placeholder"]];
return cell;
}
I hope this sample code will help you to accomplish your needs easily.
Please ask if anything look ambiguous to you.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.leadsTableView deselectRowAtIndexPath:indexPath animated:YES];
id object = [[self.tableData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
self.leadforLeadDetails = (Lead*)object;
self.selectedLeadID = self.leadforLeadDetails.ID;
[self performSegueWithIdentifier:#"openLeadDetails" sender:self];
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//NSIndexPath *indexPath = [self.tableView2 indexPathForCell:sender];
//[self.tableView2 deselectRowAtIndexPath:indexPath animated:YES];
if ([segue.identifier isEqualToString:#"openLeadDetails"])
{
LeadsDetailsTableViewController *destViewController = segue.destinationViewController;
destViewController.dictLeadDetails = self.dictLeadDetails;
destViewController.leadID = self.leadforLeadDetails.ID;
destViewController.Name = self.leadforLeadDetails.Name;
destViewController.leadDetails.ID = self.leadforLeadDetails.ID;
destViewController.leadDetails.Name = self.leadforLeadDetails.Name;
}
}
You could either:
Define a segue in your storyboard's prototype for the cell in question and you don't have to write a didSelectRowAtIndexPath at all;
Have didSelectRowAtIndexPath do a performSegueWithIdentifier (giving it the unique string "storyboard identifier" that you set up for your segue in the storyboard;
You can then write a prepareSegue that would look at the table's indexPathForSelectedRow to identify what row was selected. You can then grab the appropriate information from your app's model and set the appropriate property in the destinationViewController.
You could also have didSelectRowAtIndexPath do an instantiateViewControllerWithIdentifier set the property, and then do the appropriate pushViewController or presentViewController, but I personally prefer the above techniques that leverage the segues you've defined in your storyboard.

Releasing view from stack, tab bar with nav controller

So I am working on a vital update to an app I have on the appstore. I have an app with a tab bar and a nav controller. When the user selects an item from the list it sends the link it is getting from the xml file sent from my server to a detail view controller that is only a web view. The problem I'm having is when the user goes back to the tableview which is the root view for that tab the detail view isn't being released. When you select the other options in the app, the detail view doesn't change. It's not that my data isn't being released from the uishared application data but its the view isn't releasing. I know that there are many similar items like this on here and i've tried all of them. I will give you some of my code and greatly appreciate other tips and tricks. Im 15 and just getting into development so any info helps. Heres the code i think is necessary for anyone to help.
TableViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSString * storyLink = [[stories objectAtIndex: storyIndex] objectForKey: #"link"];
// clean up the link - get rid of spaces, returns, and tabs...
storyLink = [storyLink stringByReplacingOccurrencesOfString:#" " withString:#""];
storyLink = [storyLink stringByReplacingOccurrencesOfString:#"\n" withString:#""];
storyLink = [storyLink stringByReplacingOccurrencesOfString:#" " withString:#""];
videoDetailViewController.title = #"Videos";
ExampleAppDataObject* theDataObject = [self theAppDataObject];
theDataObject.videoData = storyLink;
if (self.videoDetailViewController == nil)
{
VideoDetailViewController *aBookDetail = [[[VideoDetailViewController alloc] initWithNibName:#"VideosDetailView" bundle:[NSBundle mainBundle]] autorelease];
self.videoDetailViewController = aBookDetail;
[aBookDetail release];
aBookDetail = nil;
}
[self.navigationController pushViewController:videoDetailViewController animated:YES];
}
DetailViewController:
#import "VideoDetailViewController.h"
#import "RSSEntry.h"
#import "ExampleAppDataObject.h"
#import "AppDelegateProtocol.h"
#implementation VideoDetailViewController
#synthesize activityIndicator;
#synthesize webView;
#synthesize pasteboard;
- (ExampleAppDataObject*) theAppDataObject;
{
id<AppDelegateProtocol> theDelegate = (id<AppDelegateProtocol>) [UIApplication sharedApplication].delegate;
ExampleAppDataObject* theDataObject;
theDataObject = (ExampleAppDataObject*) theDelegate.theAppDataObject;
return theDataObject;
}
- (void)viewDidLoad
{
ExampleAppDataObject* theDataObject = [self theAppDataObject];
NSString *urladdress = theDataObject.videoData;
NSURL *url = [NSURL URLWithString:urladdress];
NSURLRequest *requestobj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestobj];
pasteboard = [UIPasteboard generalPasteboard];
[super viewDidLoad];
}
- (void)dealloc
{
[webView release];
webView = nil;
[activityIndicator release];
[pasteboard release];
[VideoDetailViewController release];
[urlData release];
urlData = nil;
[super dealloc];
}
#end
I skipped a lot of the code that i didnt feel was necessary. If you want the actual files then email me at evan.stoddard#me.com
Don't release the webview in the detailViewController page, instead, put
[webView loadRequest:requestobj]; in in viewDidAppear(), not viewDidLoad
Also:
One way to make the transition smoother, change:
if (self.videoDetailViewController == nil) {
VideoDetailViewController *aBookDetail = [[[VideoDetailViewController alloc] initWithNibName:#"VideosDetailView" bundle:[NSBundle mainBundle]] autorelease];
self.videoDetailViewController = aBookDetail;
[aBookDetail release];
aBookDetail = nil;
}
[self.navigationController pushViewController:videoDetailViewController animated:YES];
Should be:
VideoDetailViewController *aBookDetail = [[VideoDetailViewController alloc] initWithNibName:#"VideosDetailView" bundle:[NSBundle mainBundle]] ;
[self.navigationController pushViewController:videoDetailViewController animated:YES];
[aBookDetail release];
Thanks to Anna Billstrom, I realized that I also should be using viewDidAppear instead of viewDidLoad. This is because I am passing data to the controller through an external data class when an item is selected from the table view. Therefore, the detail view controller is loading before there is data to load from the external data class. viewDidAppear solves this problem by getting data once the view loads after the cell is selected and there is data in the external class.

iOS - UITableView Pushing URL to UIWebView in Different View Controller

new to this site, but my head is absolutely killing me after trying for the past few hours to fix something that I know has to be insanely simple.
Short story:
I have a UIViewController with a UINavigationController
The UIViewController (tableView) is a UITableView that parses and displays RSS feed stories
There is a second UIViewController (newsWebView) with a UIWebView that gets pushed by the UINavigationController and is meant to display each story as the user clicks on rows in tableView
When the user is done with the UIWebView, they are pushed back to the first view, tableView
I have everything working except for the push of each row's URL to the webView in the second controller. It pushes the view, but the URL just isn't getting sent to the second controller so the webView is blank. The NSLogs I have in the code indicate that the URL is being created successfully, too, so what do I need to do to get newsWebView to handle the URL?
tableView.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSString * storyLink = [[stories objectAtIndex: storyIndex] objectForKey: #"link"];
// clean up the link - get rid of spaces, returns, and tabs...
storyLink = [storyLink stringByReplacingOccurrencesOfString:#" " withString:#""];
storyLink = [storyLink stringByReplacingOccurrencesOfString:#"\n" withString:#""];
storyLink = [storyLink stringByReplacingOccurrencesOfString:#" " withString:#""];
NSLog(#"storyLink: %#", storyLink);
if (storyLink != nil && ![storyLink isEqualToString:#""]) {
// Wrapping the troublesome call with logs so you should be able to see if this is the line that is killing your app
NSLog(#"about to create url");
NSURL *url = [NSURL URLWithString:storyLink];
NSLog(#"creating url was successful");
//URL Request Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView
newsWebView *detailViewController = [[newsWebView alloc] initWithNibName:#"newsDetailWebView" bundle:nil];
//detailViewController.item = [[stories objectAtIndex: storyIndex] objectForKey: #"link"];
[self.navigationController pushViewController:detailViewController animated:YES];
//[detailViewController loadRequest:requestObj];
[detailViewController release];
}
}
So, what do I need to add to viewDidLoad in newsWebView.m? Or, for that matter, what is wrong with my tableView's didSelectRowAtIndexPath?
newsWebView.m
- (void)viewDidLoad {
}
Thank you very, very much. I need to take some Advil!
Have an instance variable say urlObject of NSUrl type in your newsWebView class. Make sure you add #property and #synthesize the object.
then
newsWebView *detailViewController = [[newsWebView alloc] initWithNibName:#"newsDetailWebView" bundle:nil];
newsWebview.urlObject = url;
[self.navigationController pushViewController:detailViewController animated:YES];
//[detailViewController loadRequest:requestObj];
[detailViewController release];
And in newsWebView.m
- (void)viewDidLoad {
[webView loadRequest:[NSURLRequest requestWithURL:urlObject]];
}

How To Load Plist Into Multiple Table Views

I made a data.plist and want to load the root array (names) into a table view, then when you click on each cell, it will load their corresponding exercise array into a new table view. How would I implement this method into my TableViewController?
My current plist has a root array, with a dictionary item for each muscle group (Item 0, then child name) then contains an array for the muscle's exercises.
My didSelectRowAtIndexPath Method for the first table that pushes the 2nd view subtable is:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
ExerciseTableViewController *detailViewController = [[ExerciseTableViewController alloc] initWithNibName:#"AbdominalTableViewController" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
My viewDidLoad method for the 2nd table view controller (this needs to be fixed) is:
- (void)viewDidLoad
if (exerciseArray == nil)
{
NSString *path = [[NSBundle mainBundle]pathForResource:#"biceps" ofType:#"plist"];
NSMutableArray *array = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.exerciseArray = array;
[array release];
Assuming i understand you question.
You can do something like:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *muscle = [muscles objectAtIndex:indexPath.row]; //'muscles' array is the first level of your plist
...
ExerciseTableViewController *detailViewController = [[ExerciseTableViewController alloc] initWithNibName:muscle bundle:nil];
detailViewController.muscle = muscle;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
...
}
And in the second controller:
...
NSString *path = [[NSBundle mainBundle]pathForResource:#"biceps" ofType:#"plist"];
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
self.exerciseArray = [plistDict objectForKey:muscle]; //'muscle' is the string you set in the previous controller
[plistDict release];
...
Hope it's help.
Edit after Faisal comment:
Using the following plist file:
For read this plist you can do:
//Read the plist file into an array (the root element is an array)
NSString *path = [[NSBundle mainBundle]pathForResource:#"Data" ofType:#"plist"];
NSArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
Then for read the second level, the 'rootLevel' content:
NSDictionary *secondLevel = [rootLevel objectAtIndex:0];
NSString *muscle = [secondLevel valueForKey:#"name"]; //"Biceps" string
NSArray *exercises = [secondLevel valueForKey:#"exercises"]; //[exercises objectAtIndex:0] is "Biceps Curl" string