gpt4 book ai didi

ios - 自定义委托(delegate)不响应 iOS

转载 作者:行者123 更新时间:2023-11-29 10:45:56 25 4
gpt4 key购买 nike

我已经设置了一个委托(delegate)来获取仪表的旧位置和新位置。但是,我似乎无法得到委托(delegate)的回应。我可能会遗漏一些东西,但我已经做了很多研究,看起来一切都井井有条。

MeterView.h

#import <UIKit/UIKit.h>
#import "KTOneFingerRotationGestureRecognizer.h"

@protocol MeterViewDelegate;


@interface MeterView : UIView{

IBOutlet UIImageView *gauge;
IBOutlet UIImageView *needle;
float rotation, oldPos, newPos;
KTOneFingerRotationGestureRecognizer *rgest;
}

@property (nonatomic, weak) id<MeterViewDelegate> delegate;

- (IBAction)handleMovedNeedle;

@end

@protocol MeterViewDelegate <NSObject>

- (void)meterView:(MeterView*)view OldValue:(float)oldval NewValue:(float)newval;

@end

MeterView.m

#import "MeterView.h"
#import <QuartzCore/QuartzCore.h>


@implementation MeterView

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {

}
return self;
}

-(void)handleMovedNeedle{
if ([self.delegate respondsToSelector:@selector(meterView:OldValue:NewValue:)]) {
[self.delegate meterView:self OldValue:oldPos NewValue:newPos];
}
else{
NSLog(@"Delegate call FAIL, needle moved from %f, to %f", oldPos,newPos);
}
}

-(void)awakeFromNib{

gauge = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gauge.png"]];
needle = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"needle.png"]];
needle.frame = CGRectMake(0,gauge.frame.size.height-(needle.frame.size.height/2), gauge.frame.size.width, needle.frame.size.height);

[self addSubview:gauge];
[self addSubview:needle];
rotation = 0;
rgest = [[KTOneFingerRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
rgest.center = CGPointMake(CGRectGetMidX([needle bounds]) + needle.frame.origin.x, CGRectGetMidY([needle bounds]) + needle.frame.origin.y);

[self addGestureRecognizer:rgest];

}

- (void)rotate:(UIRotationGestureRecognizer *)recognizer {
switch (recognizer.state) {
case UIGestureRecognizerStateBegan: {
oldPos = ([(NSNumber *)[needle.layer valueForKeyPath:@"transform.rotation.z"] floatValue]/3.14)*100;
}
break;
case UIGestureRecognizerStateChanged: {

CGFloat angle = [(NSNumber *)[needle.layer valueForKeyPath:@"transform.rotation.z"] floatValue];

angle += [recognizer rotation];
if (angle >= 0 && angle <= M_PI) {
[needle setTransform:CGAffineTransformRotate([needle transform], [recognizer rotation])];
rotation += [recognizer rotation];
}

}
break;
case UIGestureRecognizerStateEnded: {
newPos = ([(NSNumber *)[needle.layer valueForKeyPath:@"transform.rotation.z"] floatValue]/3.14)*100;
[self handleMovedNeedle];

}
break;
default:
break;
}

}

@end

ViewController.h

#import <UIKit/UIKit.h>
#import "MeterView.h"
#import "KTOneFingerRotationGestureRecognizer.h"

@interface ViewController : UIViewController<MeterViewDelegate>{

IBOutlet MeterView *secondMeter;
IBOutlet MeterView *thirdMeter;

}

@end

ViewController.m

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>

#define DEG2RAD(degrees) (degrees * 0.01745327) // degrees * pi over 180
#define RAD2DEG(radians) (radians * 57.2957795) // radians * 180 over pi

@interface ViewController ()

@end

@implementation ViewController


- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
secondMeter = [[MeterView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
secondMeter.delegate = self;
thirdMeter = [[MeterView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
thirdMeter.delegate =self;
}

-(void)meterView:(MeterView *)view OldValue:(float)oldval NewValue:(float)newval{
NSLog(@"Delegate call SUCCESS, need moved from %f, to %f", oldval,newval);
}


- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

最佳答案

这段代码:

secondMeter = [[MeterView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
secondMeter.delegate = self;
thirdMeter = [[MeterView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
thirdMeter.delegate =self;

创建您的 View 的 2 个实例,但不会将它们添加到 View 中,因此它们永远不会被看到。

因此,您可能有一些其他 View 实例在 View 中但没有委托(delegate),而这些 View 有委托(delegate)但没有显示。大概来自 XIB/ Storyboard,因为你有网点。

使用 socket 将显示的 View 连接到委托(delegate),而不是创建新实例。

如果您将 View 中的委托(delegate)属性设为 IBOutlet 本身,那么您可以将 View Controller 连接为 XIB/ Storyboard 中的委托(delegate),而无需担心代码中的委托(delegate)完全...

关于ios - 自定义委托(delegate)不响应 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22389920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com