1
我有一個視圖(View1,分配給View1Controller)在我的iPad上打開。我怎樣才能得到它打開View2(View2Controller)並關閉自己...我相信它是簡單的,但我管理彈出,並找不到如此簡單的東西。如果可能的話,動畫過渡將是更可取的。在iPad上打開新視圖,並關閉現有視圖。
感謝您的幫助
我有一個視圖(View1,分配給View1Controller)在我的iPad上打開。我怎樣才能得到它打開View2(View2Controller)並關閉自己...我相信它是簡單的,但我管理彈出,並找不到如此簡單的東西。如果可能的話,動畫過渡將是更可取的。在iPad上打開新視圖,並關閉現有視圖。
感謝您的幫助
下面是您可以完成此操作的一種方法的大致輪廓。
假設您的AppDelegate中有屬性,名爲viewController1
和viewController2
(這些應該是nonatomic, retain
以使內存管理更容易)。
當你想切換從viewController1
你需要刪除做viewController2
以下
// Remove the old view
[self.viewController1.view removeFromSuperView];
// Release it's controller (just to economize on memory)
self.viewController1.view = nil;
if(self.viewController2 == nil)
{
// Load the new controller from it's NIB/XIB
ViewController2* vc2 = [[ViewController2 alloc]
initWithNibName:@"ViewController2"
bundle:nil];
// Assign it to property & release to keep memory management clean
self.viewController2 = vc2;
[vc2 release];
}
// Add the new view controller to the window
[self.window addSubview:vc2];
一個最簡單的方式來通知AppDelegate中切換視圖是使用通知中心。
由於您保留屬性中的視圖控制器,不要忘記將它們釋放到您的dealloc
中。 希望這可以幫助你。
如果我的視圖是在沒有nib的情況下創建的,而是由代碼創建的,該怎麼辦? – David26th 2011-05-13 05:09:20
只需要替換initWithNib,但是您在問題中創建了「ViewController1」和「ViewController2」。 (雖然即使視圖是在代碼中創建的,您也應該能夠將'nil'傳遞給'initWithNibName',這會導致您的控制器的'loadView'被調用,這是您應該創建編程視圖的地方。 – idz 2011-05-13 05:13:32