从另一个视图控制器中删除视图控制器 [英] Remove view controller from another view controller

查看:39
本文介绍了从另一个视图控制器中删除视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am very new to iPhone app development.
I am developing one example application for iPhone emulator using Objective-C++ and std CPP.

我的应用程序中有两个视图,在来自 CPP 代码的某些事件上,我使用第一个视图控制器中的以下代码显示第二个视图.

I have two views in my application, on some events from CPP code i am displaying second view using following code from the first view controller.

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

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

I am able to see second view coming on screen, but problem is that i am unable to end/remove second view controller from first view controller.

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

How can i remove second view controller from first view controller using second view controller's pointer or using any other method.

要删除第二个视图,我在第二个视图控制器文件中有以下代码,该代码在第二个视图的按钮单击事件上调用.

To remove second view i have following code in second view controller file, which gets called on button click event of second view.

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

以上代码完美运行,当我点击秒视图的结束按钮时,它会从屏幕上移除第二个视图控制器并导航到第一个视图,我如何使用相同的代码从第一个视图控制器中移除第二个视图.

Above code works perfectly, when i click on the seconds view's end button it removes the second view controller from the screen and navigets to first view, how can i use same code to remove second view from the first view controller.

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

I tied to use NSNotificationCenter to send event from first view to second view to call the function onEndBtnClicked but it is not working.

正确的做法是什么?

OSX 版本:10.5.8 和 Xcode 版本:3.1.3

OSX version: 10.5.8 and Xcode version: 3.1.3

推荐答案

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

In the secondViewController create a protocol like:

@protocol SecondViewScreenControllerDelegate <NSObject>

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

// Any other button possibilities

@end

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

Now you have to add a property in the secondViewController class:

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

您在 secondViewController 实现中对其进行合成:

You sinthesize it in the secondViewController implementation:

@synthesize delegate = _delegate;

最后你要做的就是在你的 firstViewController 中实现协议并在呈现它之前正确设置 secondViewController:

Finally all you have to do is implement the protocol in your firstViewController and set the secondViewController properly prior presenting it:

@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 时:

Then when presenting the secondViewController from the first:

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 实现中)

And ready. Whenever you want to dismiss the secondViewController from the first, just call: (inside the secondViewController implementation)

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

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

All that happens is that you send a pointer of the secondViewController that you can use from the first. Then you can work with it without problem. No C++ needed. In Cocoa you won't need C++. Almost everything can be done with Objective-C, and it's more dynamic.

这篇关于从另一个视图控制器中删除视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆