Search code examples
iphoneuinavigationcontrolleruitabbarcontrolleruitableview

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

Solution

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