Frage

I have a couple of "ViewControllers" and one for Update a picture in an iOS app. The first one has a button when tapped asks if the user wants to use gallery photo or camera. Now i am presenting this controller by using presentViewController on self.

But when the second view controller is presented i want to set the UIImagePicker source according to what the user has passed in.

I have made 2 different methods. one with camera source and one with "photoslibrary". I don't know how to invoke one of these methods based on the use choice from the previous controller. Am i going the right way with this approach? or should i just have one controller?

War es hilfreich?

Lösung

Basically there are two ways of passing data to a view controller.


Storyboard
If you are working with storyboard segues (that is, control drag from your "root" view controller to the destination view controller, choose a transition style and define a identifier), you can present the view controller via

[self performSegueWithIdentifier:@"yourSegueIdentifier" sender:self];

Most of the times your destination view controller will be a custom class, so define a public property to hold the data you want to pass through. Then implement the following in your "root" view controller

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Setup the location menu delegate
    if([segue.identifier isEqualToString:@"yourSegueIdentifier"]) {
        // The custom class of your destination view controller,
        // don't forget to import the corresponding header
        ViewControllerCustomClass *vc = segue.destinationViewController;
        // Set custom property
        vc.chosenImageId = self.chosenImageId;
        // Send message
        [vc message];
   }
}

Hints:
If your destination view controller is the root view controller of a navigationViewController you can access it via [[segue.destinationViewController childViewControllers] objectAtIndex:0]; Additionally, as senderis an id, you can "abuse" it to pass any object through, just as a NSDictionary, for example.
Also note, that when I am referring to the root view controller, I am talking of the view controller from which we segue to the destination from.


Programmatically

ViewControllerCustomClass *vc = [[ViewControllerCustomClass alloc] init];
vc.chosenImageId = self.chosenImageId;

// If you want to push it to the navigation controller
[self.navigationController pushViewController:vc animated:YES];

// If you want to open it modally
[self presentViewController:vc animated:YES completion:nil];

Andere Tipps

You can use inheritance, make your previous controller superClass, and invoke method in presentViewController in viewDidload.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top