本文介绍了打开和关闭其他 UIViewControllers - 除了使用协议 & 之外的任何其他方法代表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 FirstVC 屏幕中打开另一个屏幕的正常方法是这样的:

The normal way to open another screen from within a FirstVC screen, so one can close it again is like this:

    SecondVC *secondVC = [[SecondVC alloc] initWithNibName:@"SecondVC" bundle:nil];
    secondVC.delegate = self; //needed to dismiss
    [self presentModalViewController: secondVC animated: YES];

SecondVC.m 必须导入一个协议,该协议声明调用关闭 SecondVC 的方法

while the SecondVC.m has to import a protocol that declares the method called to close the SecondVC

所以我总是要创建一个协议文件 SecondVCProtocol.h,它基本上看起来像这样:

So I always have to create a protocol file SecondVCProtocol.h which basically looks like this:

@protocol SecondVCProtocol <NSObject>
-(void)secondVCDidFinish;
@end

然后在 SecondVC.m 我需要导入这个 SecondVCProtocol.h 文件,现在终于可以调用了

Then in SecondVC.m I need to import this SecondVCProtocol.h file and now can finally call

 [self.delegate    secondVCDidFinish]

我刚刚完成了另一个 Android 应用程序,回到 iOS 世界,我觉得这很麻烦.- 需要在单独的文件中定义这样的协议 &需要使用委托 - 只是为了完成最正常的任务,比如关闭屏幕......

I have just completed another Android app and beeing back in the iOS world, I find this rather cumbersome. - needing to define such a protocol in a separate file & needing to use a delegate - all just to do the most normal task like closing a screen...

难道没有更简单、不那么复杂的方法吗?还是必须这样做?

例如像 SecondVC 中的 [self dismiss] - 没有委托,没有协议 - 他不是很好吗?

for example like [self dismiss] in SecondVC - no delegate, no protocol - wouldn't his be really nice?

非常感谢!

推荐答案

你可以直接调用

dismissViewControllerAnimated:completion:

在呈现的视图控制器上,虽然这并不是最佳实践.

on the presented viewcontroller, although it is not exactly best practice.

来自 Apple 的文档:

From Apple's documentation:

呈现视图控制器负责关闭视图它呈现的控制器.如果您在呈现的视图上调用此方法控制器本身,它会自动将消息转发到呈现视图控制器.

也来自 Apple 的文档(http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html)

Also from Apple's documentation though (http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html)

当需要关闭呈现的视图控制器时,首选方法是让呈现视图控制器关闭它.换句话说,只要有可能,同一个视图控制器提出的视图控制器也应该负责解雇它.虽然有几种技术可以通知呈现视图控制器,其呈现的视图控制器应该被解雇,首选技术是委托.

这篇关于打开和关闭其他 UIViewControllers - 除了使用协议 &amp; 之外的任何其他方法代表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-13 21:59