Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Delegate Methods Not Working iOS

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:

Container and UIView

I cannot reach the setNewBackgroundColour and have been looking for answers for hours. I cannot see what I have done wrong?

Sam

like image 360
samb90 Avatar asked Aug 02 '26 23:08

samb90


1 Answers

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;
like image 121
rdelmar Avatar answered Aug 05 '26 13:08

rdelmar