Releasing view from stack, tab bar with nav controller - iphone

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.

Related

QLPreviewController - Displaying issue

code snippets
NSMutableArray *samplepdf = [[NSMutableArray alloc]initWithObjects:#"sam1.pdf",#"sam2.pdf",#"sam3.pdf",#"sam4.pdf", nil];
1) QLPreviewController *previewController = [[QLPreviewController alloc] init];
previewController.dataSource = self;
previewController.currentPreviewItemIndex = [indexPath row];
[self presentModalViewController:previewController animated:YES];
2)
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)previewController
{
return [samplepdf count];
}
// returns the item that the preview controller should preview
- (id)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)index
{
NSString *documentsDirectoryPath = [[NSBundle mainBundle]resourcePath];
NSString *dataPath =[documentsDirectoryPath stringByAppendingPathComponent:[samplepdf objectAtIndex:index]];
NSURL *url = [NSURL fileURLWithPath:dataPath isDirectory:YES];
return url;
}
This code Work perfectly in iOS5 ,but when iam running in ios6 some space is visible.
How can i solve this issue?
Thanks in Advance :)
The call to presentModalViewController:animated: is deprecated in iOS6. You might want to specify a modal transition style and modal presentation style prior to calling presentViewController:animated:. You could try something like this:
[previewController setModalPresentationStyle:UIModalPresentationFullScreen];
[previewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentViewController:previewController animated:YES completion:nil];

UIDocumentInteractionController presentPreviewAnimated: returning NO and not showing preview

I've tried everything I can think of and it still responds NO and Apple's doesn't have any hints.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"finished!");
NSString *tempString = [NSString stringWithFormat:#"file://%#%#", NSTemporaryDirectory(), [[NSProcessInfo processInfo] globallyUniqueString]];
NSURL *tempURL = [NSURL URLWithString:tempString];
[receivedData writeToURL:tempURL atomically:YES];
NSLog(#"tempURL is written!");
UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL:tempURL];
interactionController.delegate = self;
[interactionController retain];
NSLog(#"--- %i", [interactionController presentPreviewAnimated:YES]);
NSLog(#"presented preview");
[connection release];
[receivedData release];
}
- (UIViewController*)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
NSLog(#"asdf");
UIViewController *viewController = [[[UIViewController alloc] init] autorelease];
[self.navigationController pushViewController:viewController animated:YES];
return viewController;
}
1) are you confident the document you're opening is kosher? If not, then returning NO would be the thing to expect
2) I'm puzzled by your delegate method. The present method is happy to do the pushing onto the navigation controller all by itself. Do you fair any better if you use this code instead? (See the docs for rationale.)
- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller
{
return [self navigationController];
}
I had a similar issue with presentPreviewAnimated which returned NO. In my case I was missing an extension of the file. When I renamed my file from document to document.xlsx it was opened successfully.

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]];
}

call NSDictionary in current view controller

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];}

Label text not initialising in new view iphone

DetailViewController.m
Code:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *navTitle = self.navigationItem.title;
NSString *stalklabel = self.labelforurl.text;
NSLog(#"%#", stalklabel);
NSURL *address = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.twostepmedia.co.uk/json.php?q=%#",stalklabel]];
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:address];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
//NSString *address = [NSString stringWithFormat:#"http://www.twostepmedia.co.uk/json.php?q=%#",na];
//[self initiateRequest:address];
//NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:address]];
//[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
RootViewController.m
Code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//Initialize the detail view controller and display it.
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:dvController animated:YES];
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
dvController.navigationItem.title=[[managedObject valueForKey:#"data"] description];
dvController.labelforurl.text = [[managedObject valueForKey:#"data"] description];
[dvController release];
dvController = nil;
}
The output of NSLOG is "Label"?? So when i try and embed it in the URL, it only comes back with "Label"??
Any ideas?
In InterfaceBuilder, or somewhere in code, your labelforurl has a value of Label. Change this value for a different result. If you want to change it in IB, use a UITextField instead of a UIlabel.