Objective c 从另一个视图控制器中删除视图控制器

Objective c 从另一个视图控制器中删除视图控制器,objective-c,ios,uiviewcontroller,Objective C,Ios,Uiviewcontroller,我对iPhone应用程序开发非常陌生。 我正在使用Objective-C++和std CPP为iPhone emulator开发一个示例应用程序 我的应用程序中有两个视图,关于CPP代码中的一些事件,我使用第一个视图控制器中的以下代码显示第二个视图 // Defined in .h file secondViewScreenController *mSecondViewScreen; // .mm file Code gets called based on event from CPP (

我对iPhone应用程序开发非常陌生。 我正在使用Objective-C++和std CPP为iPhone emulator开发一个示例应用程序

我的应用程序中有两个视图,关于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];
}
上面的代码工作得很好,当我单击秒视图的结束按钮时,它将从屏幕上删除第二个视图控制器,然后导航到第一个视图,如何使用相同的代码从第一个视图控制器中删除第二个视图

我尝试使用NSNotificationCenter将事件从第一个视图发送到第二个视图,以调用函数onEndBtnClicked,但它不起作用

正确的做法是什么


OSX版本:10.5.8和Xcode版本:3.1.3

如果应用程序中只有两个视图,则使用

- (IBAction)onEndBtnClicked:(UIButton *)sender
{
   [self dismissModalViewControllerAnimated:NO];
}
删除下面的行:

 [self.navigationController popViewControllerAnimated:YES];

既然您拒绝了第二个视图,那么为什么要将其从第一个视图中删除呢

如果应用程序中只有两个视图,则使用

- (IBAction)onEndBtnClicked:(UIButton *)sender
{
   [self dismissModalViewControllerAnimated:NO];
}
删除下面的行:

 [self.navigationController popViewControllerAnimated:YES];

既然您拒绝了第二个视图,那么为什么要将其从第一个视图中删除呢

在secondViewController中创建如下协议:

@protocol SecondViewScreenControllerDelegate <NSObject>

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

// Any other button possibilities

@end
最后,您所要做的就是在第一个ViewController中实现协议,并在演示之前正确设置第二个ViewController:

@interface firstViewController : UIViewController <SecondViewScreenControllerDelegate>
然后,从第一个视图显示第二个ViewController时:

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

所发生的一切就是您发送了第二个ViewController的指针,您可以从第一个ViewController开始使用它。然后你就可以毫无问题地使用它了。不需要C++。在可可中,你不需要C++。几乎所有事情都可以用Objective-C完成,而且它更具动态性。

在secondViewController中创建一个协议,如:

@protocol SecondViewScreenControllerDelegate <NSObject>

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

// Any other button possibilities

@end
最后,您所要做的就是在第一个ViewController中实现协议,并在演示之前正确设置第二个ViewController:

@interface firstViewController : UIViewController <SecondViewScreenControllerDelegate>
然后,从第一个视图显示第二个ViewController时:

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

所发生的一切就是您发送了第二个ViewController的指针,您可以从第一个ViewController开始使用它。然后你就可以毫无问题地使用它了。不需要C++。在可可中,你不需要C++。Objective-C几乎可以完成所有操作,而且更具动态性。

在使用NSNotificationCenter时也显示部件。在使用NSNotificationCenter时也显示部件。为什么要从第一视图中删除它。对于CPP代码中的某些事件,我要删除第二视图,这些事件在第一视图控制器中接收,因此我需要从第一视图控制器中删除第二视图。为什么要从第一视图中删除它。对于CPP代码中的某些事件,我要删除第二视图,这些事件在first view controller中收到,因此我需要从first view controller中删除第二个视图。感谢您的回复,我将尝试您的建议并让您知道结果。我将CPP与Objective-C一起使用,因为我有本机CPP代码,该代码具有我的应用程序的所有基本功能,我希望重用它。添加代码后我会出现编译错误,添加@property弱、非原子id委托后我会出现错误;到secondViewController.h文件。你能不能建议一个完整的例子,因为我不知道在哪里添加上面的代码行。我想我可能犯了一些错误。我认为使用这两个UIViewController属性更容易:presentingViewController和presentedViewController。编辑secondViewController实现时:使用self。presentingViewController或[self presentingViewController]获取第一个。但是,如果只需要关闭视图控制器,则可以使用第二个:[self dismissViewControllerAnimated:YES completion:NULL]关闭视图控制器。如果你有更多的问题,发布整个代码。谢谢你的回复,我会尝试你的建议,并让你知道结果。我将CPP与Objective-C一起使用,因为我有本机CPP代码,该代码具有我的应用程序的所有基本功能,我希望重用它。添加代码后我会出现编译错误,添加@property弱、非原子id委托后我会出现错误;到secondViewController.h文件。你能推荐一个com吗
请举个例子,因为我不知道在哪里确切地添加上面的代码行。我想我可能犯了一些错误。我认为使用这两个UIViewController属性更容易:presentingViewController和presentedViewController。编辑secondViewController实现时:使用self。presentingViewController或[self presentingViewController]获取第一个。但是,如果只需要关闭视图控制器,则可以使用第二个:[self dismissViewControllerAnimated:YES completion:NULL]关闭视图控制器。如果您有更多问题,请发布整个代码。