How to use presentModalViewController in iPhone/iPad?

1.2k views Asked by At

Hi i am very new in iPhone/iPad developmet.

In my application on clicking of button in want to show view controller like presentModalViewController and i am able to do that which contains the UITableView with some numbers of values. on selecting particulate row i want to pass values to controller which is behind that controller.

for that i am using apple sample application PhotoPicker code. http://developer.apple.com/library/ios/#samplecode/PhotoPicker/Introduction/Intro.html

But i am not able to understand the what i did wrong in my code.

I am not able to go in the code which is in the MyViewController.m

- (void)didFinishWithCamera
{
    [self dismissModalViewControllerAnimated:YES];
//Here is my some logic
}

can any one help me for this...how to call this function from OverlayViewController?

please refer above link and guide me or give me some steps or guide me for the same.

2

There are 2 answers

1
Matthias Bauch On

use delegation.

I use something like this in a app I'm writing at the moment:

// MySecretSelectionViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [delegate mySecretSelectionViewController:self didSelectObject:[self objectForIndexPath:indexPath] atIndexPath:indexPath];
}

// MyViewController.m
- (void)mySecretSelectionViewController:(MySecretSelectionViewController *)es didSelectObject:(MySecretObject *)object atIndexPath:(NSIndexPath *)indexPath {
    // do something with the selected object
    [self dismissModalViewControllerAnimated:YES];
}

- (void)showMySecretSelectionViewController:(id)sender {
    MySecretSelectionViewController *vc = ...
    vc.delegate = self;
    // present ViewController
}
1
Elias On

You can also do this with use of NSNotificationCenter.

Inside MyViewController.m:

- (void)viewDidLoad 
{
    // your code

    // Add observers
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishWithCamera) name:@"YourObserverName" object:nil];
}

+ (void)callDidFinishWithCamera
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"YourObserverName" object:nil];
}

- (void)dealloc 
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];

    // your code
}

From OverlayViewController.m:

[MyViewController callDidFinishWithCamera];

Use the above class method to call didFinishWithCamera in MyViewController from OverlayViewController