Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I implement delegation for a view controller presented by a segue?

In my main view controller, an NSTimer calls the function below after a specified delay:

-(void)showAdView{
    [self performSegueWithIdentifier:@"ShowAdSegue" sender:nil];        
}

This modally presents MyAdViewController, and I'd like the MyAdViewController class to have a MyAdViewControllerDelegate protocol, so that I can implement an adViewDidFinish function in my main view controller.

Is it possible to do that without creating a new instance of MyAdViewController in the main view controller?

like image 835
Eric Avatar asked Dec 03 '25 15:12

Eric


1 Answers

You can do the following:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
  [segue.destinationViewController setDelegate:self]; 
}

For this to work you have to include your view controller header file in the view controller executing the segue and also make the delegate property of your MyAdViewController public by adding it to the header file of the class.

like image 92
8vius Avatar answered Dec 06 '25 04:12

8vius