7

我有一個tabBarController,我通過將下面的代碼添加:iPhone - 通過UITabBarItem和dismissModalViewController presentModalViewController乾淨

AppDelegate.h:

... 
    UITabBarController IBOutlet *tabBarController; 
} 

@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController; 

AppDelegate.m:

... 
    [self.window addSubview:tabBarController.view]; 
    [self.window makeKeyAndVisible]; 
    [tabBarController setDelegate:self]; 

然後我用下面的代碼來呈現一個模式條形碼掃描的視圖控制器:

- (void)tabBarController:(UITabBarController *)tbc didSelectViewController:(UIViewController *)vc { 
     // Middle tab bar item in question. 
     if (vc == [tabBarController.viewControllers objectAtIndex:2]) { 
      ScanVC *scanView = [[ScanVC alloc] initWithNibName:@"ScanViewController" bundle:nil]; 

      // set properties of scanView's ivars, etc 

      UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:scanView]; 

      [tabBarController presentModalViewController:navigationController animated:YES]; 
      [navigationController release]; 
      [scanView release]; 
     } 
    } 

當它真正得到展示我覺得這個方法不夠吸引人,因爲當我關閉該模式的看法,我把回到空視圖。

許多條碼掃描應用程序或應用程序,例如簡單地顯示圖像選擇器;做得很成功。我只是想知道爲了達到這樣的效果他們會用什麼樣的實現。

這就是所謂的路徑的應用程序的截圖,其中有完全相同的功能,我以後:

alt text

我還注意到,在這些應用中,如果你在其他任何比中間的一個標籤欄項目,我們可以說,並且您單擊顯示模式視圖的標籤欄項目,一旦它被解散它實際上不會將它們帶回到它視爲正常的空白視圖,但實際選項卡顯示模態視圖的酒吧項目從未被選中。如果這是實現這種類型效果的唯一方法,我會對這種類型的功能感到滿意。

任何幫助將不勝感激,因爲我一直堅持了很長一段時間。此外,我甚至不確定是否將所有代碼放入我的AppDelegate以便將View Controller作爲模式呈現的正確方法。這一切似乎都是錯的。

+1

我想你會發現Path應用程序使用自己的標籤欄控制器實現。即不是來自Cocoa Touch的`UITabBarController` – ohhorob 2011-01-17 04:21:52

回答

1

當用戶單擊標籤欄項目時,您不應該呈現模式視圖。

您可以改爲在其中一個選項卡提供的視圖中顯示模式視圖。或者,如果您只有一個主視圖和想要以模態方式呈現的掃描視圖,則應該只使用一個按鈕在主視圖中顯示掃描視圖。相反,您可以使用帶有單個按鈕的工具欄。

+0

看看應用程序,例如Path和Stickybits,以獲得我所追求的功能。 – fuzz 2011-01-16 03:11:31

+0

在我原來的問題中查看上面的截圖。 – fuzz 2011-01-16 03:23:17

2

當您關閉模態視圖控制器,告訴標籤欄選擇任何選項卡中最初選擇。

- (void)dismissModalViewControllerAnimated:(BOOL)animated 
    { 
     // do whatever you need to do when dismissing 

     // savedTabIndex is an int ivar 
     // tabBarController is a reference, set when showing the modal view 
     [[self tabBarController] setSelectedIndex:savedTabIndex]; 
    } 

你將不得不保存在變量原來的標籤欄選擇在tabBarController:didSelectViewController:開始。

- (void)tabBarController:(UITabBarController *)tbc 
didSelectViewController:(UIViewController *)vc 
{ 
    // Save the tab bar index (if it's not the photo tab) 
    if ([tabBarController selectedIndex] != 3]) { 
     savedTabIndex = [tabBarController selectedIndex]; 
    } 
} 

這段代碼可能存在錯誤,我只是在沒有測試的情況下輸入它。

1

我在UITabBarControllerDelegate附近發現了一個非常簡單的解決方案 - 我只在iOS 7上試過。

首先,子類UITabBarController,使其成爲它自己的UITabBarControllerDelegate,並創建一個屬性,該屬性將保存對要啓動模式的選項卡的引用。在我的應用程序中,它被稱爲「銷售」選項卡。

@property (strong, nonatomic) UIViewController *sellTab; 

然後,在你init方法,只需創建一個視圖控制器,並把它添加到標籤。

_sellTab = [[UIViewController alloc] init]; 
_sellTab.title = @"Sell"; 
self.viewControllers = @[homeTab, historyTab, _sellTab, bookmarksTab, profileTab]; 

現在這裏是魔術的地方:覆蓋下面的標籤欄控制器委託方法。代碼非常明瞭。

#pragma mark - Tab bar controller delegate 

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController 
{ 
    return viewController != self.sellTab; 
} 

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item 
{ 
    if (item == self.sellTab.tabBarItem) { 
     [self presentViewController:[[UINavigationController alloc] initWithRootViewController:[[PostAdViewController alloc] init]] animated:YES completion:nil]; 
    } 
} 

這將啓動一個模式,解僱後顯示您在啓動之前的相同標籤。