2013-04-26 30 views
0

我是iOS編程的新手,我正在使用ECSlidingViewController來創建滑出菜單(如Facebook)。想象一下,如果我在菜單中引用了兩個視圖。如何獲得已經實例化的UIViewController?

當我打開應用程序時,顯然會調用viewDidLoad作爲我的頂視圖(我的菜單中的第一個視圖)。如果我打開菜單並選擇第二個視圖,它也會爲此調用viewDidLoad。但是,如果我回到第一個視圖,它會再次調用該方法,這是我不想要的。我有一些設置代碼,如果可能,我不想重新實例化視圖。我見過Facebook,並且他們沒有重新表達觀點,因爲它記住了我在牆上的滾動位置,例如,在我切換視圖並返回之後。

這是被選擇後,觸發我的委託方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Get identifier from selected 
    NSString *identifier = [NSString stringWithFormat:@"%@", [self.menu objectAtIndex:indexPath.row]]; 

    // Add the selected view to the top view 
    UIViewController *newTopVC = [self.storyboard instantiateViewControllerWithIdentifier:identifier]; 

    // Present it 
    [self.slidingViewController anchorTopViewOffScreenTo:ECRight animations:nil onComplete:^{ 
     CGRect frame = self.slidingViewController.topViewController.view.frame; 
     self.slidingViewController.topViewController = newTopVC; 
     self.slidingViewController.topViewController.view.frame = frame; 
     [self.slidingViewController resetTopView]; 
    }]; 
} 

有沒有辦法以某種方式得到一定的VC,如果包括已經創造了它?這樣,它只會多次呼叫viewWillAppear,而不是viewDidLoad

謝謝。

+2

是的,在第一次創建它時創建一個強屬性,然後使用該屬性而不是在下次需要時創建新屬性。 – rdelmar 2013-04-26 18:45:57

+0

@rdelmar那麼這將是一個'UIViewController'數組,因爲我有多個視圖? – swiftcode 2013-04-26 18:48:17

回答

1

可以使用navigationcontroller。當您想要轉到第二個視圖時,可以將其推送到導航視圖控制器上,當您返回時,可以將其從導航控制器中彈出。

編輯:

如果你有3個意見,你仍然可以使用navigationcontroller。與上述相同的邏輯。但請記住刪除導航控制器中同一個viewcontroller的雙重實例。看這個頁面:How to remove a specific view controller from uinavigationcontroller stack?。檢查特定的viewcontroller是否存在,如果是這樣,刪除然後將它推到最上面。

+0

但是如果我有兩個以上的意見呢?我不知道這是否會奏效。例如,如果我有3個視圖。我加載1,然後轉到3,然後想要轉到2.之後,我想再次轉到1。這將如何解決? – swiftcode 2013-04-26 19:05:53

+0

看到編輯..希望它可以幫助... – lakesh 2013-04-26 19:17:09

+0

這是NavigationController在代碼中創建某種堆棧?我知道有一個UINavigationController,但我的邏輯是真的只保存和加載視圖狀態,而不是塞格和所有的東西,因爲我的菜單實現了導航功能。 – swiftcode 2013-04-26 19:39:36

相關問題