我要在IOS版本開發一個應用,但是我是安卓開發的,如何在IOS中實現一個類似startActivityForResult的效果?需要展示一個新的view controller,然後在新view controller關閉時候向當前view controller返回一個control。同時還需要觸發一個回調方法。
如何在ios中實現這些?
有很多種方法實現。
首先,在應用的delegate中建立一個導航控制器:
self.viewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
self.navigationController = [[ UINavigationController alloc ] initWithRootViewController:self.viewController ];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
展示新的view controller:
OtherViewController *ovc = [[ OtherViewController alloc ] initWithNibName:@"OtherViewController" bundle:nil ];
[ self.navigationController pushViewController:ovc animated:YES ];
返回:
[ self.navigationController popViewControllerAnimated:YES ];
等到回調之後,在工程中加一個protocol:
@protocol AbstractViewControllerDelegate <NSObject>
@required
- (void)abstractViewControllerDone;
@end
然後對需要回調的view controller 進行delegate觸發:
@interface OtherViewController : UIViewController <AbstractViewControllerDelegate>
@property (nonatomic, assign) id<AbstractViewControllerDelegate> delegate;
@end
最後,獲得一個新的view controller:
OtherViewController *ovc = [[ OtherViewController alloc ] initWithNibName:@"OtherViewController" bundle:nil ];
ovc.delegate = self;
[ self.navigationController pushViewController:ovc animated:YES ];
等到關閉了viewcontroller,進行:
[self.delegate abstractViewControllerDone];
[ self.navigationController popViewControllerAnimated:YES ];
在符合剛剛那個protocol的根viewcontroller中,加上這個方法:
-(void) abstractViewControllerDone {
}
這樣你就做了一個調用,還要很多設置,根據你自己的需求。