[英]Custom Delegate Methods Not Working iOS
I created a custom delegate method to update the background colour of a ViewController. 我创建了一个自定义委托方法来更新ViewController的背景颜色。 I cannot get the delegate method to respond. 我无法得到委托方法来回应。 Here is my code: 这是我的代码:
VASettingsView.h 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 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 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 VARendererViewController.m
ViewDidLoad viewDidLoad中
// Setup UIView to be delegate
VASettingsView *settingsView = [[VASettingsView alloc] init];
settingsView.delegate = self;
setNewBackgroundColour 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. 我无法到达setNewBackgroundColour并且一直在寻找答案。 I cannot see what I have done wrong? 我看不出我做错了什么?
Sam 山姆
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. 我怀疑问题是你在VARendererViewController中初始化一个新的VASettingsView实例,而不是获得一个你正在屏幕上的指针。 Since you don't show how you get VASettingsView on screen, or how you create its view, it's only a guess. 由于您没有显示如何在屏幕上显示VASettingsView,或者您如何创建其视图,因此这只是猜测。
After edit: 编辑后:
Instead of, 代替,
VASettingsView *settingsView = [[VASettingsView alloc] init];
settingsView.delegate = self;
Try this, 尝试这个,
VASettingsView *settingsView = self.childViewControllers[0];
SettingsView.delegate = self;
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
. 如果您使用的是iOS 6.0 SDK,则问题可能是委托属性未链接到实例变量delegate
。 Apple changed the standard so that if you don't @synthesize
the property, the corresponding instance variable will be called _delegate
. Apple改变了标准,因此如果你没有@synthesize
属性,相应的实例变量将被称为_delegate
。
To fix your problem, try either synthesizing the delegate
property or accessing it by calling self.delegate
in the saveButtonPressed:
method. 要解决您的问题,请尝试合成delegate
属性或通过在saveButtonPressed:
方法中调用self.delegate
来访问它。
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. 退出viewDidLoad
,该变量消失。 You need to make VASettingsView *settingsView
an instance variable (ie declare it in your implementation), and then set its delegate in viewDidLoad
. 您需要使VASettingsView *settingsView
实例变量(即在您的实现中声明它),然后在viewDidLoad
设置其委托。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.