有一个问题我一直在努力,而且我似乎无法在网上获得足够的信息。它与Apple多任务处理和标签栏视图有关。我想控制在应用程序进入前台或变为活动状态时选择哪个选项卡。 每当我关闭应用程序并重新打开它时,它将打开显示在选项卡栏上的最后一个UIView。我怎么能控制它?
首先,我希望每次应用程序变为活动状态时都能显示带有应用徽标的预览图像。这是我正在使用的代码,但是当应用程序进入此行的前景时,我一直得到EXC_BAD_ACCESS:[window addSubview:self.preMiniView]:
- (void)applicationDidBecomeActive:(UIApplication *)application {
if (!self.preMiniView)
{
self.preMiniView = [[UIImageView alloc] initWithFrame:window.frame];
self.preMiniView.frame = CGRectMake(0,0,324,480);
window.backgroundColor = [UIColor colorWithRed:21.0/255.0 green:7.0/255.0 blue:2.0/255.0 alpha:1];
}
UIImage *img = [UIImage imageNamed:@"preloadSm.jpg"];
self.preMiniView.image = img;
[img release];
if (self.preMiniView.alpha == 0.0)
{
[self.preMiniView setAlpha:1.0];
}
[window addSubview:self.preMiniView]; // adding the how to view to the view.
[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(initApp:)
userInfo:nil
repeats:NO];
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[tabBarController.view endEditing:YES];
[tabBarController.view setAlpha:0.0];
[self.preMiniView release];
self.preMiniView = nil;
NSLog(@"applicationDidEnterBackground iphone");
}
- (void)initApp:(NSTimer *)timer
{
if (self.preMiniView)
{
[NSTimer scheduledTimerWithTimeInterval:0.5
target:self
selector:@selector(removePrev:)
userInfo:nil
repeats:NO];
[window insertSubview:tabBarController.view atIndex:0];
[tabBarController.view setAlpha:1.0];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.7];
[self.preMiniView setAlpha:0.0];
[UIView commitAnimations];
}
}
- (void)removePrev:(NSTimer *)timer
{
[self.preMiniView removeFromSuperview];
}
其次,当我的应用程序访问方法应用程序时,我正在尝试使用我的一个UIViews呈现pdf文件:openURL:sourceApplication:annotation(用于从电子邮件应用程序打开文件)。我得到了正确的文件URL,但我不确定如何在我的一个名为PDFReaderViewController的自定义UIView中显示实际文件。看来我不能在AppDelegate上使用这段代码:
pdfController = [[PDFReaderViewController alloc] init];
[pdfController initFromName:fileName];
[[self navigationController] pushViewController:pdfController animated:YES];
[pdfController release];
pdfController = nil;
我收到警告:接收者'PDFReaderViewController'是一个转发类,相应的@interface可能不存在
知道如何使用AppDelegate中的PDFReaderViewController显示新文件吗?
我必须遗漏一些基本的东西....感谢您的帮助!
答案 0 :(得分:0)
我想指出您没有正确遵循一些内存管理规则。点符号表示您正在访问一个属性,应该为您处理内存管理,这可能是您EXC_BAD_ACCESS的原因。
//You need to autorelease the image view here
self.preMiniView = [[UIImageView alloc] initWithFrame:window.frame];
...
//Never release a property directly
[self.preMiniView release];
//This is the correct way to release the property
self.preMiniView = nil;
...
//This creates autoreleased image
UIImage *img = [UIImage imageNamed:@"preloadSm.jpg"];
//This will increment retain count
self.preMiniView.image = img;
//This is reason for EXC_BAD_ACCESS do not release you never retained it yourself
[img release];
关于转发类引用的警告意味着在头文件中你可能有一些像@class PDFReaderViewController;
这样的代码,并且从不在实现类中包含正确的头。