我是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
。
謝謝。
是的,在第一次創建它時創建一個強屬性,然後使用該屬性而不是在下次需要時創建新屬性。 – rdelmar 2013-04-26 18:45:57
@rdelmar那麼這將是一個'UIViewController'數組,因爲我有多個視圖? – swiftcode 2013-04-26 18:48:17