1

I have some view controller, and when I click some button I make modalView (wrapped with UINavigationController) and present it.

DscViewController *enterDescription = [[[DscViewController alloc] init] autorelease];


    UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:enterDescription];
    [self presentModalViewController:navController animated:YES];
    [navController release];

The question is, how to make my (parent)view controller to be delegate, and when I click on some button at modalViewController (Done for example) to call some method in my parent viewController to dismiss modal and do some savings from modal input?

dormitkon
  • 2,526
  • 4
  • 39
  • 60

1 Answers1

1

I don't think that it's good practice to make one controller blame its parent controller to do work that it should handel. You can always use self.parentViewController to dismiss the modal you are currently in.

If for some reason you only handle storage in the parentViewController then you can point your done button action to a method in your current viewController and then use self.parentViewController to trigger the parent method for storage.

or just set your buttons delegate directly to self.parentViewController

[button addTarget:self.parentViewController action:@selector(someMethodInParentViewController) forControlEvents:UIControlEventTouchUpInside];
Cyprian
  • 9,423
  • 4
  • 39
  • 73
  • I got "unrecognized selector sent to instance..." error with this showed example. Of course, in my parent view controller I have method -(void)someMethodInPrentView – dormitkon Jul 18 '11 at 07:22
  • This is because, my modalViewController is wrapped with NavigationController – dormitkon Jul 18 '11 at 07:28
  • You can always pass your root viewController by reference to you modal viewController that is contained in the UINavigationViewController. You can also use NSNotifications, without using delegates. – Cyprian Jul 18 '11 at 08:38
  • Can you give me some simple example for first solution? – dormitkon Jul 18 '11 at 12:28
  • 1
    Look at my answer [here](http://stackoverflow.com/questions/5873450/calling-method-in-current-view-controller-from-app-delegate-in-ios/5873554#5873554) for using NSNotifications – Cyprian Jul 18 '11 at 12:31