Question

I have storyboard with two views, one is main second is modal. So when i tap button in first view i call my modal view(no code, all in storyboard) and i need to close my modal view using a delegate. So in my first view i write

First.h

#import "Modal.h"

@interface SkyViewController: UIViewController <ModalDelegate>

First.m

in viewDidLoad
ModalView *modal = [[Modal alloc] init];
modal.delegate = self;

-(void)exit
{
   [self dismissModalViewControllerAnimated:YES];
}

Modal View

Modal.h

@protocol ModalDelegate;

@interface ModalView : UIViewController

....

@property (assign) id<ModalDelegate> delegate;

.....

@protocol ModalDelegate

-(void)exit;

@end

in main

Modal.m

....
@syntesize delegate;

...

-(void)buttonPressed
{
   [delegate exit];
}

what am i doing wrong? i see in google that it can bee some problem in owners in storyboard... I don't know :(

Was it helpful?

Solution

The problem is

  ModalView *modal = [[Modal alloc] init]. 

This creates a new instance of your modal view, not the one you get on screen with a button click. You should implement prepareForSegue, and set your delegate there. ModalView will be the segue.destinationViewController.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    ModalView *modal = segue.destinationViewController;
    modal.delegate = self;
}

This code goes in your first controller.

OTHER TIPS

You should make the delegate weak instead of 'assign'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top