iOS:如何实现Android的startActivityForResult等行为 [英] iOS: How to achieve behavior like Android's startActivityForResult
问题描述
我是Android应用程序的iOS版本开发人员。我需要知道如何在Android上实现类似于startActivityForResult的行为。我需要显示一个新的视图控制器,然后在新的视图控制器关闭时控制返回到前一个视图控制器。我还需要一个当时触发的回调方法。
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];
然后当你想要一个新的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 ];
就回调问题而言,一种方法是在你的某个地方制作这样的协议project:
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
最后当你提出一个新的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屋!