2012-12-24 76 views
1

我對iPhone應用程序開發非常陌生。
我正在開發一個使用Objective-C++和std CPP的iPhone模擬器的示例應用程序。從另一個視圖控制器中刪除視圖控制器

我在我的應用程序中有兩個視圖,來自CPP代碼的一些事件中,我使用第一個視圖控制器的以下代碼顯示第二個視圖。

// Defined in .h file 
secondViewScreenController *mSecondViewScreen; 

// .mm file Code gets called based on event from CPP (common interface function between Objective-C++ and CPP code) 
mSecondViewScreen = [[secondViewScreenController alloc] initWithNibName:nil bundle:nil]; 
[self presentModalViewController:mSecondViewScreen animated:YES]; 

我能看到第二視圖上的屏幕來了,但問題是,我無法停止/從第一視圖控制器刪除第二個視圖控制器。

如何從第一個視圖控制器使用第二個視圖控制器的指針或使用任何其他方法刪除第二個視圖控制器。

要刪除第二個視圖,我有第二個視圖控制器文件中的以下代碼,它被第二個視圖的按鈕單擊事件調用。

// In .mm of second view controller. 
- (IBAction)onEndBtnClicked:(UIButton *)sender 
{ 
    [self dismissModalViewControllerAnimated:NO]; 
    [self.navigationController popViewControllerAnimated:YES]; 
} 

上面的代碼工作完美,當我在秒視圖的結束按鈕,單擊它從屏幕移開並navigets先來看第二個視圖控制器,我怎麼能使用相同的代碼來從第一視圖中刪除第二視圖控制器。

我綁定使用NSNotificationCenter將事件從第一個視圖發送到第二個視圖來調用功能onEndBtnClicked但它不起作用。

這樣做的正確方法是什麼?

OSX版本:10.5.8和Xcode的版本:3.1.3

+0

顯示使用NSNotificationCenter時的部分。 –

回答

4

在secondViewController創建這樣一個協議:

@protocol SecondViewScreenControllerDelegate <NSObject> 

- (void)secondViewScreenControllerDidPressCancelButton:(UIViewController *)viewController sender:(id)sender; 

// Any other button possibilities 

@end 

現在你必須在secondViewController類添加屬性:

@property (weak, nonatomic) id<SecondViewScreenControllerDelegate> delegate; 

您在secondViewController實現sinthesize它:

@synthesize delegate = _delegate; 

最後,你所要做的就是在協議中實現您的firstViewController並正確設置secondViewController之前提出它:

@interface firstViewController : UIViewController <SecondViewScreenControllerDelegate> 

...

@implementation firstViewController 

    - (void)secondViewScreenControllerDidPressCancelButton:(UIViewController *)viewController sender:(id)sender 
    { 
     // Do something with the sender if needed 
     [viewController dismissViewControllerAnimated:YES completion:NULL]; 
    } 

然後從第一呈現secondViewController時:

UIViewController *sec = [[SecondViewController alloc] init]; // If you don't need any nib don't call the method, use init instead 
sec.delegate = self; 
[self presentViewController:sec animated:YES completion:NULL]; 

並準備就緒。每當你想解僱從第一secondViewController,只要致電:(該secondViewController裏面執行)

[self.delegate secondViewScreenControllerDidPressCancelButton:self sender:nil]; // Use nil or any other object to send as a sender 

所發生的一切是你送,你可以從第一次使用的secondViewController的指針。然後你可以毫無問題地使用它。不需要C++。在Cocoa中,你不需要C++。幾乎所有的東西都可以用Objective-C完成,而且更加動態。

+0

感謝您的回覆,我會嘗試您的建議並讓您知道結果。我正在使用Objective-C的CPP,因爲我有原生(CPP)代碼,它具有我應用程序的所有基本功能,我想重複使用它。 – User7723337

+0

它工作嗎?請回答:) –

+0

我在添加代碼後出現編譯錯誤,添加「@property(weak,nonatomic)id delegate後出現錯誤;」到secondViewController.h文件。你能爲此提出一個完整的例子,因爲我不知道在哪裏準確地添加上面的代碼行。我想我可能會犯一些錯誤。 – User7723337

0

如果只有兩個在你的應用程序的意見,然後用下面

- (IBAction)onEndBtnClicked:(UIButton *)sender 
{ 
    [self dismissModalViewControllerAnimated:NO]; 
} 

刪除線:

[self.navigationController popViewControllerAnimated:YES]; 

因爲它是你駁回第二個觀點,那麼你爲什麼要從第一個視圖中刪除它。

+0

「爲什麼你想從第一個視圖中刪除它」,在CPP代碼的一些事件中,我想要刪除第二個視圖,那些事件在第一個視圖控制器中被接收,因此我需要從第一個視圖控制器中刪除第二個視圖。 – User7723337

相關問題