因此,我正在对应用程序商店中的一个应用程序进行重要更新。我有一个带有选项卡栏和导航控制器的应用程序。当用户从列表中选择一个项目时,它会将从我的服务器发送的 xml 文件中获取的链接发送到仅是 Web 视图的详细视图控制器。我遇到的问题是,当用户返回到表视图(该选项卡的根视图)时,详细视图不会被释放。当您在应用程序中选择其他选项时,详细信息视图不会更改。这并不是说我的数据没有从 uishared 应用程序数据中释放,而是视图没有释放。我知道这里有很多类似的项目,我已经尝试过所有这些。我将为您提供一些我的代码,并非常感谢其他提示和技巧。我 15 岁,刚刚进入开发阶段,所以任何信息都会有所帮助。这是我认为任何人都需要提供帮助的代码。
表格视图控制器
- (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];
}
详细视图控制器:
#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
在detailViewController页面不要释放webview,而是将
[webView loadRequest:requestobj];
在viewDidAppear()中,而不是viewDidLoad
还有: 使过渡更平稳的一种方法是改变:
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];
应该是:
VideoDetailViewController *aBookDetail = [[VideoDetailViewController alloc] initWithNibName:@"VideosDetailView" bundle:[NSBundle mainBundle]] ;
[self.navigationController pushViewController:videoDetailViewController animated:YES];
[aBookDetail release];
感谢 Anna Billstrom,我意识到我也应该使用
viewDidAppear
而不是 viewDidLoad
。这是因为当从表视图中选择一个项目时,我通过外部数据类将数据传递到控制器。因此,详细视图控制器在从外部数据类加载数据之前加载。 viewDidAppear 通过在选择单元格后加载视图并且外部类中有数据时获取数据来解决这个问题。