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 co
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];
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 ];
To go back do this:
[ 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
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 ];
then when you dismiss the ovc, make this call
[self.delegate abstractViewControllerDone];
[ self.navigationController popViewControllerAnimated:YES ];
And in the rootVC, which conforms to the protocol you made, you just fill out this method:
-(void) abstractViewControllerDone {
}
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.
Generally you can use UINavigationController
to enclose your chain of viewControllers
. You can then navigate back and forth between viewControllers
. For callback you can use viewWillDissapear:
delegate method on your second view controller and do some action from there.
If we assume you want to open an activity from your own application, then it's easy. An android activity can be represented by a view controller (UIViewController
).
The architectures of iOS and Android are very different. The activities on Android are independent, the controllers on iOS are tightly connected in an application. You have to decide how to show the controller on the screen (usually using a UINavigationController
, or present it modally using presentViewController:animated:
) and connect it to the parent controller somehow to receive the result. A delegate pattern is most appropiate for this.
If you want to start an activity defined in another application or start a system activity (e.g. to take a camera picture), you have to use one of the predefined controllers (e.g. UIImagePickerController). On iOS you can't simply use controllers from a different applications in the same way as Android does.
And I can recommend you another thing - don't write an iOS app with Android design patterns. Think about what is common on iOS and implement the UI that way. Don't just copy Android code.