iOS:如何实现Android的startActivityForResult等行为

Jam*_*rpe 14 iphone android ios

我是Android应用程序的iOS版本开发人员.我需要知道如何在Android上实现类似于startActivityForResult的行为.我需要显示一个新的视图控制器,然后在新视图控制器关闭时控制返回到前一个视图控制器.我还需要一个当时触发的回调方法.

我怎样才能在iOS中实现这一目标?

Gre*_*ice 14

有几种方法,所以大多数情况下你自己用各种模式做到这一点.您可以在应用程序委托中设置导航控制器,如下所示:

self.viewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
self.navigationController = [[ UINavigationController alloc ] initWithRootViewController:self.viewController ];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
Run Code Online (Sandbox Code Playgroud)

然后,当您想要呈现一个新的vc时,您可以这样做:

OtherViewController *ovc = [[ OtherViewController alloc ] initWithNibName:@"OtherViewController" bundle:nil ];
[ self.navigationController pushViewController:ovc animated:YES ];
Run Code Online (Sandbox Code Playgroud)

要回去做:

[ self.navigationController popViewControllerAnimated:YES ];
Run Code Online (Sandbox Code Playgroud)

就回调而言,一种方法是在项目的某个地方制作这样的协议:

@protocol AbstractViewControllerDelegate <NSObject>

@required
- (void)abstractViewControllerDone;

@end
Run Code Online (Sandbox Code Playgroud)

然后让每个视图控制器都想要在委托中触发回调:

 @interface OtherViewController : UIViewController <AbstractViewControllerDelegate>

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

 @end
Run Code Online (Sandbox Code Playgroud)

最后,当您呈现新的vc时,将其指定为委托:

  OtherViewController *ovc = [[ OtherViewController alloc ] initWithNibName:@"OtherViewController" bundle:nil ];
  ovc.delegate = self;
  [ self.navigationController pushViewController:ovc animated:YES ];
Run Code Online (Sandbox Code Playgroud)

然后当你解雇ovc时,拨打这个电话

 [self.delegate abstractViewControllerDone];
 [ self.navigationController popViewControllerAnimated:YES ];
Run Code Online (Sandbox Code Playgroud)

在rootVC中,它符合您所制定的协议,您只需填写此方法:

 -(void) abstractViewControllerDone {

 }
Run Code Online (Sandbox Code Playgroud)

你刚刚打过电话.这需要大量的设置,但其他选项包括查看NSNotifications和块,这可以更简单,具体取决于你做什么.