从另一个视图控制器移除视图控制器

我对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]; 

我能够看到第二个视图在屏幕上,但问题是,我无法结束/从第一个视图控制器中删除第二个视图控制器。

如何从第一个视图控制器使用第二个视图控制器的指针或使用任何其他方法删除第二个视图控制器。

要删除第二个视图,我有第二个视图控制器文件中的第二个视图的button单击事件调用后面的代码。

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

上面的代码完美地工作,当我点击秒视图的结束button,它将第二个视图控制器从屏幕和导航到第一个视图,我怎样才能使用相同的代码从第一个视图控制器中删除第二个视图。

我绑定使用NSNotificationCenter发送事件从第一个视图到第二个视图调用onEndBtnClicked函数,但它不工作。

什么是正确的做法呢?

OSX版本:10.5.8和Xcode版本:3.1.3

在第二个ViewController中创build一个协议,如:

 @protocol SecondViewScreenControllerDelegate <NSObject> - (void)secondViewScreenControllerDidPressCancelButton:(UIViewController *)viewController sender:(id)sender; // Any other button possibilities @end 

现在你必须在secondViewController类中添加一个属性:

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

你在第二个ViewController实现中使用它:

 @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]; 

准备好了 每当你想从第一个closuressecondViewController时,只需调用:(在secondViewController实现中)

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

发生的一切就是发送第一个可以使用的secondViewController的指针。 那么你可以毫无问题地使用它。 没有C ++需要。 在Cocoa中,你不需要C ++。 几乎所有的东西都可以用Objective-C完成,而且更加dynamic。

如果您的应用程序中只有两个视图,请使用

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

删除下面的行:

  [self.navigationController popViewControllerAnimated:YES]; 

因为它是你驳回第二个观点,那么你为什么要从第一个视图中删除它。