3

我创建了一个自定义委托方法来更新 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;

设置新背景颜色

- (void)setNewBackgroundColour:(GLKVector4)newColour{

    self.backgroundColour = GLKVector4Make(newColour.x,
                                           newColour.y,
                                           newColour.z,
                                           newColour.w
                                           );
    NSLog(@"STOP");
}

故事板:

容器和 UIView

我无法到达 setNewBackgroundColour 并且已经寻找了几个小时的答案。我看不到我做错了什么?

山姆

4

3 回答 3

7

我怀疑问题是你在 VARendererViewController 中分配了一个新的 VASettingsView 实例,而不是获得一个你放在屏幕上的指针。由于您没有显示如何在屏幕上获得 VASettingsView,或者如何创建它的视图,因此这只是一个猜测。

编辑后:

代替,

VASettingsView *settingsView = [[VASettingsView alloc] init];
settingsView.delegate = self;

试试这个,

VASettingsView *settingsView = self.childViewControllers[0];
SettingsView.delegate = self;
于 2013-04-03T20:41:57.143 回答
1

如果您使用的是 iOS 6.0 SDK,问题可能是委托属性未链接到实例变量delegate。Apple 更改了标准,如果您不@synthesize使用该属性,则会调用相应的实例变量_delegate

要解决您的问题,请尝试合成delegate属性或通过调用方法来访问self.delegatesaveButtonPressed:

于 2013-04-03T20:21:37.157 回答
0

您的问题是您在方法中声明了一个局部变量,并设置了它的委托。当您 exitviewDidLoad时,该变量消失了。您需要创建VASettingsView *settingsView一个实例变量(即在您的实现中声明它),然后将其委托设置为viewDidLoad.

于 2013-04-03T20:43:24.933 回答