Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: How to achieve behavior like Android's 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.

How can I achieve this in iOS?

like image 916
James Harpe Avatar asked Oct 22 '12 14:10

James Harpe


People also ask

How to manage startactivityforresult on Android?

This example demonstrate about How to manage startActivityForResult on Android Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken text view to show on Activity result data.

How to start activity from intent in Android SDK?

The source activity call, startActivityForResult by sending in the intent together with the requestCode to Android SDK. Android SDK then opens the activity accordingly as stated in the Intent.

How to show text view on activity result data in Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken text view to show on Activity result data. Step 3 − Add the following code to src/MainActivity.java

How to get the activity result from the Caller activity?

1 The source activity call, startActivityForResult by sending in the intent together with the requestCode to Android SDK. 2 Android SDK then opens the activity accordingly as stated in the Intent. 3 Once the destination activity has finish with its job, it returns to its caller activity. ...


2 Answers

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.

like image 197
Greg Price Avatar answered Oct 20 '22 18:10

Greg Price


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.

like image 45
Sulthan Avatar answered Oct 20 '22 19:10

Sulthan