iOS的:如何获得了这幅像Android的startActivityForResult行为这幅、获得了、行为、iOS

2023-09-06 00:19:10 作者:啪啪啪

我是一名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.