Reputation: 1073
I created a custom delegate method to update the background colour of a ViewController. I cannot get the delegate method to respond. Here is my code:
VASettingsView.h
#import <UIKit/UIKit.h>
#import <GLKit/GLKit.h>
@class VASettingsView;
@protocol VASettingsViewDelegate <NSObject>
- (void)setNewBackgroundColour:(GLKVector4)newColour;
@end
@interface VASettingsView : UIView {
id <VASettingsViewDelegate> delegate;
}
@property (strong, nonatomic) IBOutlet UIButton *blackBackgroundButton;
@property (strong, nonatomic) IBOutlet UIButton *saveButton;
@property GLKVector4 backgroundColourSetting;
// Set delegate method
@property (nonatomic,weak)id delegate;
- (IBAction)blackButtonPressed:(id)sender;
- (IBAction)saveButtonPressed:(id)sender;
@end
VASettingsView.m
#import "VASettingsView.h"
@implementation VASettingsView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (IBAction)blackButtonPressed:(id)sender {
self.backgroundColourSetting = GLKVector4Make(0.0, 0.0, 0.0, 0.0);
}
- (IBAction)saveButtonPressed:(id)sender {
[delegate setNewBackgroundColour:self.backgroundColourSetting];
}
@end
VARendererViewController.h
@class VA_CASFrame;
@class VA_Character;
@class VA_Orbit_Camera;
#import <UIKit/UIKit.h>
#import <GLKit/GLKit.h>
#import <QuartzCore/QuartzCore.h>
#import "VASettingsView.h"
@interface VARendererViewController : GLKViewController <VASettingsViewDelegate>
{
// Code
}
VARendererViewController.m
ViewDidLoad
// Setup UIView to be delegate
VASettingsView *settingsView = [[VASettingsView alloc] init];
settingsView.delegate = self;
setNewBackgroundColour
- (void)setNewBackgroundColour:(GLKVector4)newColour{
self.backgroundColour = GLKVector4Make(newColour.x,
newColour.y,
newColour.z,
newColour.w
);
NSLog(@"STOP");
}
Storyboard:
I cannot reach the setNewBackgroundColour and have been looking for answers for hours. I cannot see what I have done wrong?
Sam
Upvotes: 3
Views: 4464
Reputation: 104092
I suspect the problem is that you are alloc init'ing a new instance of VASettingsView in VARendererViewController, rather than getting a pointer the one that you're putting on screen. Since you don't show how you get VASettingsView on screen, or how you create its view, it's only a guess.
After edit:
Instead of,
VASettingsView *settingsView = [[VASettingsView alloc] init];
settingsView.delegate = self;
Try this,
VASettingsView *settingsView = self.childViewControllers[0];
SettingsView.delegate = self;
Upvotes: 7
Reputation: 1767
Your problem is that you declared a local variable inside a method, and set its delegate. By the time you exit viewDidLoad
, that variable disappears. You need to make VASettingsView *settingsView
an instance variable (i.e. declare it in your implementation), and then set its delegate in viewDidLoad
.
Upvotes: 0
Reputation: 66
If you're using the iOS 6.0 SDK, the problem could be that the delegate property is not linked to the instance variable delegate
. Apple changed the standard so that if you don't @synthesize
the property, the corresponding instance variable will be called _delegate
.
To fix your problem, try either synthesizing the delegate
property or accessing it by calling self.delegate
in the saveButtonPressed:
method.
Upvotes: 1