打开和关闭其他UIViewControllers - 任何其他方法,而不是使用协议和代表? [英] opening and closing other UIViewControllers - any other approaches than to use protocol & delegate?

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

问题描述

  

从FirstVC屏幕中打开另一个屏幕的正常方式, secondVC * secondVC = [[SecondVC alloc] initWithNibName:@SecondVCbundle:nil];
secondVC.delegate = self; //需要关闭
[self presentModalViewController:secondVC animated:YES];

SecondVC.m 必须导入一个协议声明了关闭SecondVC的方法



所以我总是要创建一个协议文件 SecondVCProtocol.h 基本上如下所示:

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

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

  [self.delegate secondVCDidFinish] 

我刚刚完成了另一个Android应用程序, iOS世界,我觉得这很麻烦。 - 需要在单独的文件&需要使用一个代表 - 只需要做最正常的任务,如关闭一个屏幕...



是不是有一个更简单的不那么复杂的方法这只是一个必须完成的方式?



例如像 [self dismiss] SecondVC - 没有代理,没有协议 - 他不会真的很好吗?



非常感谢!

解决方案

您可以调用

  dismissViewControllerAnimated:completion:

在所呈现的viewcontroller上,虽然不是最好的做法。



从苹果公司文档:


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


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


当它来了时间来驳回一个呈现的视图控制器,
首选方法是让呈现视图控制器关闭
它。换句话说,只要有可能,
提交视图控制器的同一个视图控制器也应该负责
关闭它。虽然有几种技术通知
呈现视图控制器,其显示的视图控制器应该
被解雇,但首选技术是委托。



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

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

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

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

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

 [self.delegate    secondVCDidFinish]

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...

Isn't there an easier less complex way or is this just the way it has to be done?

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

Many thanks!

解决方案

You can just call

dismissViewControllerAnimated:completion:

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

From Apple's documentation:

The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, it automatically forwards the message to the presenting view controller.

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

When it comes time to dismiss a presented view controller, the preferred approach is to let the presenting view controller dismiss it. In other words, whenever possible, the same view controller that presented the view controller should also take responsibility for dismissing it. Although there are several techniques for notifying the presenting view controller that its presented view controller should be dismissed, the preferred technique is delegation.

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

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