2011-03-23 46 views
0

有一個問題,我一直在努力,我似乎​​無法獲得關於它在網絡上的足夠的信息。它與Apple多任務處理和Tab欄視圖有關。我想控制當應用程序進入前景或變爲活動時將選擇哪個選項卡。 目前每次關閉應用程序並重新打開應用程序,它都會打開標籤欄上顯示的最後一個UIView。我如何控制它?如何使用UITabBarController在applicationDidBecomeActive上顯示視圖

首先,我希望能夠在每次應用程序激活時顯示帶有應用程序徽標的預覽圖像。下面是我使用的代碼,但我不斷收到EXC_BAD_ACCESS,當應用程序正進入前景這一行:[窗口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,但我不知道如何顯示實際的文件在我的自定義UIView之一稱爲PDFReaderViewController。看來我不能只是使用此代碼在AppDelegate中:

pdfController = [[PDFReaderViewController alloc] init]; 
[pdfController initFromName:fileName]; 

[[self navigationController] pushViewController:pdfController animated:YES]; 
[pdfController release]; 
pdfController = nil; 

我得到一個警告:接收機「PDFReaderViewController」是一個正向類和相應的@interface可能不存在

任何想法如何顯示從AppDelegate使用PDFReaderViewController的新文件?

我必須在這裏錯過一些基本的東西....感謝您的幫助!

回答

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;一些代碼,從來沒有包括在實現類的正確的標題。

+0

感謝您的幫助。我改變了我的代碼,它似乎工作。現在我得到'UIWindow'的警告,可能無法響應[[window navigationController] pushViewController:pdfController animated:YES]的'-navigationController'; – dynamo42 2011-03-23 18:22:43

+0

這是正確的'UIWindow'不響應導航控制器,但'UIViewController'會。如果問題對您有幫助,請務必將問題標記爲已回答。如果您遇到'-navigationController'問題,請嘗試使用代碼發佈另一個問題,我相信您會得到幫助。 – Joe 2011-03-23 18:25:27

+0

謝謝,剛纔。關於這個問題,我仍然有一個問題...我怎樣才能抓住顯示在tabBarController中的當前視圖?我想如果我能得到它我可以推動PDF視圖。再次感謝你的幫助! – dynamo42 2011-03-23 18:35:49