iOS的:如何获得了这幅像Android的startActivityForResult行为 [英] iOS: How to acheive behavior like Android's startActivityForResult

查看:275
本文介绍了iOS的:如何获得了这幅像Android的startActivityForResult行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名Android开发工作的一个版本的iOS我们的应用程序中。我需要知道如何实现类似于startActivityForResult在Android上的行为。我需要展示一个新的视图控制器,然后让控制回归到previous视图控制器,当新的视图控制器关闭。我还需要一个回调方法在那个时候被触发。

I'm an Android developer working on an iOS version of our app. I need to know how to achieve behavior similar to startActivityForResult on Android. I need to show a new view controller, then have control return to the previous view controller when the new view controller is closed. I also need a callback method to be triggered at that time.

我怎样才能在iOS中做到这一点?

How can I achieve this in iOS?

推荐答案

有一对夫妇的方式,所以大多是你自己做的各种图案。您可以设置在应用程序委托导航控制器如下:

There are a couple ways, so mostly you do this yourself with various patterns. You can set up a navigation controller in the app delegate as follows:

self.viewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
self.navigationController = [[ UINavigationController alloc ] initWithRootViewController:self.viewController ];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];

当你想present

然后一个新的VC,你可以这样做:

Then when you want to present a new vc you can do this:

OtherViewController *ovc = [[ OtherViewController alloc ] initWithNibName:@"OtherViewController" bundle:nil ];
[ self.navigationController pushViewController:ovc animated:YES ];

要回去做到这一点:

[ self.navigationController popViewControllerAnimated:YES ];

至于回调去做到这一点的方法之一是让这样的地方在你的项目的协议:

As far as a callback goes one way to do this is to make a protocol like this somewhere in your project:

@protocol AbstractViewControllerDelegate <NSObject>

@required
- (void)abstractViewControllerDone;

@end

然后,让每个视图控制器你想有一个回调的委托又名触发:

Then make each view controller you want a callback to be triggered in a delegate aka:

 @interface OtherViewController : UIViewController <AbstractViewControllerDelegate>

 @property (nonatomic, assign) id<AbstractViewControllerDelegate> delegate;

 @end

最后,当你present一个新的VC将其指定为一个委托:

Finally when you present a new vc assign it as a delegate:

  OtherViewController *ovc = [[ OtherViewController alloc ] initWithNibName:@"OtherViewController" bundle:nil ];
  ovc.delegate = self;
  [ self.navigationController pushViewController:ovc animated:YES ];

那么当您关闭了OVC,使这个电话

then when you dismiss the ovc, make this call

 [self.delegate abstractViewControllerDone];
 [ self.navigationController popViewControllerAnimated:YES ];

而在rootVC,这符合您所做的协议,您只需填写这个方法:

And in the rootVC, which conforms to the protocol you made, you just fill out this method:

 -(void) abstractViewControllerDone {

 }

您刚刚打了一个电话给。这需要大量的设置,但是其他的选择包括寻找到NSNotifications和块可以是简单的取决于你做什么。

That you just made a call to. This requires a lot of setup but other options include looking into NSNotifications and blocks which can be simpler depending on what your doing.

这篇关于iOS的:如何获得了这幅像Android的startActivityForResult行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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