Note that there are some explanatory texts on larger screens.

plurals
  1. POObjective-C Protocols Not Sending Message
    primarykey
    data
    text
    <p>I have read around, and it seems as though delegates would be really useful in my app. Unfortunately, every tutorial about protocols I have tried has failed - the delegate is not receiving the message! It would be great if someone could tell me what I'm doing wrong.</p> <p>I created a really simple test app with two ViewControllers, a FirstViewController and a SecondViewController. I have set them up in container views to see the effect properly. My Main.storyboard looks like this:</p> <p><img src="https://i.stack.imgur.com/Vf5nf.png" alt="Main.storybord"></p> <p>The purpose of the test app is to change the background colour of the SecondViewController when one of the buttons is pressed in the FirstViewController.</p> <p>Here is FirstViewController.h:</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @protocol FirstViewControllerDelegate -(void)colourDidChange:(UIColor *)theColour; @end @interface FirstViewController : UIViewController{ UIButton *redButton; UIButton *blueButton; } @property (nonatomic, retain) id &lt;FirstViewControllerDelegate&gt; delegate; @property (nonatomic, retain) IBOutlet UIButton *redButton; @property (nonatomic, retain) IBOutlet UIButton *blueButton; -(IBAction)redPressed; -(IBAction)bluePressed; </code></pre> <p>My FirstViewController.m:</p> <pre><code>#import "FirstViewController.h" @interface FirstViewController () @end @implementation FirstViewController @synthesize redButton, blueButton; @synthesize delegate; -(IBAction)redPressed{ [self.delegate colourDidChange:[UIColor redColor]]; } -(IBAction)bluePressed{ [self.delegate colourDidChange:[UIColor blueColor]]; } -(void)viewDidLoad { [super viewDidLoad]; } </code></pre> <p>I think I have implemented the protocol and the calling of the delegate correctly.</p> <p>Here is my SecondViewController.h:</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import "FirstViewController.h" @interface SecondViewController : UIViewController &lt;FirstViewControllerDelegate&gt; -(void)colourDidChange:(UIColor *)theColour; </code></pre> <p>And my SecondViewController.m:</p> <pre><code>-(void)colourDidChange:(UIColor *)theColour{ self.view.backgroundColor = theColour; } -(void)viewDidLoad { [super viewDidLoad]; FirstViewController *firstView = [[FirstViewController alloc]init]; firstView.delegate = self; } </code></pre> <p>I have breakpointed the project and realised that <code>colourDidChange:</code> in the SecondViewController is never executed. It would be much appreciated if someone could point out what I have done wrong, whether declaring (or conforming to) the delegate poorly or not setting the delegate the right way.</p> <p>Many thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload