我创建了一个自定义委托方法来更新ViewController的背景色。我无法获得委托方法来响应。这是我的代码:
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");
}
故事板:
我无法到达setNewBackgroundColour,并且一直在寻找答案数小时。我看不到我做错了什么?
山姆
最佳答案
我怀疑问题是您在VARendererViewController中分配了一个VASettingsView的新实例的初始化,而不是获得一个您要放在屏幕上的指针。由于您没有显示如何在屏幕上获得VASettingsView或如何创建其视图,因此仅是一个猜测。
编辑后:
代替,
VASettingsView *settingsView = [[VASettingsView alloc] init];
settingsView.delegate = self;
试试这个,
VASettingsView *settingsView = self.childViewControllers[0];
SettingsView.delegate = self;