2010-05-06 71 views
1

我有一個UITabBarConroller,我用它來切換3個不同的視圖。這一切都完美。在我的標籤之一,我在添加一個按鈕,所謂的「添加」,我增加了一個出口這一點,以及它看起來像下面的IBAction爲方法:iPhone SDK:切換到一個視圖,然後回到以前的視圖錯誤

// Method used to load up view where we can add a new ride 
- (IBAction)showAddNewRideView {  

    MyRidesViewController *controller = [[MyRidesViewController alloc] initWithNibName:@"AddNewRide" bundle:nil]; 
    controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
    [self presentModalViewController:controller animated:YES]; 
    [controller release]; 

}//end showAddNewRideView 

這目前工作正常,並加載我的AddNewRide nib文件。但是,一旦該視圖加載,我有一個取消按鈕,點擊後,我想返回到前一個視圖。所以,我想我會只是做上述的相反,使用下面的方法,將加載回我以前的筆尖:

- (IBAction)cancelAddingNewRide { 
    MyRidesViewController *controller = [[MyRidesViewController alloc] initWithNibName:@"MainWindow" bundle:nil]; 
    controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
    [self presentModalViewController:controller animated:YES]; 
    [controller release]; 

}//end cancelAddingNewRide 

但是,它試圖加載主窗口筆尖,程序崩潰,我也得到出現以下錯誤:

2010-05-05 20:24:37.211 Ride[6032:207] *** -[MyRidesViewController cancelAddingNewRide]: unrecognized selector sent to instance 0x501e450 
2010-05-05 20:24:37.213 Ride[6032:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[MyRidesViewController cancelAddingNewRide]: unrecognized selector sent to instance 0x501e450' 

所以,我有點失落,爲什麼它會以單向方式工作,而不是其他方式。

回答

3

首先,我想解決部分錯誤:將您的視圖看作一個堆棧。當您「推」模式控制器時,您將該視圖添加到堆棧。舊的觀點仍然存在。因此,您需要從模式視圖中「彈出」以返回到舊視圖。如果你推新視圖,你現在有3個視圖都在佔用內存,你真的只需要一個視圖。

因此,內cancelAddingNewRide只是嘗試:

[super dismissModalViewControllerAnimated:true]; 

您可能正在導致崩潰等問題,但這通常應該把事情的工作。

+0

這是事實,但不是推薦的方法。當前的viewController不應該自行解散,調用它的類應該解僱它 – Rudiger 2010-05-06 04:57:25

+0

Apple的文檔說:「父視圖控制器負責解除它使用presentModalViewController:animated:方法呈現的模式視圖控制器。但是,如果您在模態視圖控制器本身上調用此方法,則模式視圖控制器會自動將消息轉發給其父視圖控制器。「雖然模態控制器不負責任,但並不表示它不應該自行解散。請問蘋果是否在其他地方記錄這個文件? – 2010-05-06 05:18:43

+0

Yay!這絕對有效,並解決了我的問題。但是,你看到這不是一個好方法嗎? – 2010-05-06 05:33:53

0

通常,當我使用presentModalViewController時,呈現的viewController會告訴調用viewController使用dismissModalViewControllerAnimated將其關閉:YES;

換句話說,在cacncelAddingNewRide中,您只需調用showAddnewRideView中的類並讓它傳遞給方法。

它,你很難解釋,但不適顯示一個例子:

cancelAddingNewRide類:

- (IBACtion)home:(id)sender { 
    if (self.delegate respondsToSelctor:@selector(dismiss:)]) { 
     [self.delegate dismiss:self]; 
    } 
} 

,然後在showAddNewRideView類

-(void) dismiss:(cancelAddingNewRide_class *) controller { 
    [self dismissModalViewControllerAnimated:Yes]; 
} 

希望是有道理的和SOZ錯別字

編輯:哦,並使控制器的委託選擇f

controller.delegate = self; 

其實想到這件事還有更多。您必須將被調用的viewController定義爲Delegate。看看斯坦福大學的iPhone講座,第11講與此有關,並可從iTunesU

相關問題