gpt4 book ai didi

ios - 自定义委托(delegate)在 iOS 中不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:14:04 25 4
gpt4 key购买 nike

我的自定义委托(delegate)不起作用。以前我使用自定义委托(delegate),但这次它不起作用。我想将数据(更改后的 UISlider 值)从 UIViewController 传递到作为 UIView 子类的类。这是我的代码。请帮帮我。 -

RatingViewController.h -

#import <UIKit/UIKit.h>

@class StarRatingView; //define class, so protocol can see that class

@protocol DelegateForSlider <NSObject> //define delegate protocol

- (void) getValue:(CGFloat) value; //define delegate method to be implemented within another class

@end


@interface RatingViewController : UIViewController

@property (nonatomic, weak) id <DelegateForSlider> delegate; //define DelegateForSlider as delegate
@property (weak, nonatomic) IBOutlet UIView *ratingView;
- (IBAction)sliderValueChange:(id)sender;
@property (weak, nonatomic) IBOutlet UISlider *slider;

@end

RatingViewController.m -

#import "RatingViewController.h"
#import "StarRatingView.h"

@interface RatingViewController ()

@end

@implementation RatingViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

StarRatingView* starViewWithAnimated = [[StarRatingView alloc]initWithFrame:CGRectMake(5, 8, 100, 50) andRating:49];
[self.ratingView addSubview:starViewWithAnimated];
}

- (IBAction)sliderValueChange:(id)sender {
NSLog(@"Value Changed");
[self.delegate getValue:self.slider.value];
}

@end

在这个类中,我想获取 UISlider 值。StarRatingView.h -

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

@interface StarRatingView : UIView <DelegateForSlider>

- (id)initWithFrame:(CGRect)frame andRating:(int)rating;

@end

StarRatingView.m -

#import "StarRatingView.h"

@interface StarRatingView()

@property (strong, nonatomic) RatingViewController *ratingViewController;
@property (nonatomic, strong) UILabel* label;

@end

@implementation StarRatingView

- (id)initWithFrame:(CGRect)frame andRating:(int)rating {
self = [super initWithFrame:frame];
if (self) {
_ratingViewController.delegate = self;

//Add label after star view to show rating as percentage
self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,frame.size.width, frame.size.height)];
self.label.font = [UIFont systemFontOfSize:18.0f];
self.label.text = [NSString stringWithFormat:@"%d%%",rating];
self.label.textAlignment = NSTextAlignmentRight;
self.label.textColor = [UIColor whiteColor];
self.label.backgroundColor = [UIColor blueColor];
[self addSubview:self.label];
}

return self;
}

-(void) getValue:(CGFloat)value {
NSLog(@"Got Value : %f", value);//This line does print nothing, I mean never fire
}

@end

最佳答案

您需要在 RatingViewController.mviewDidLoad 方法中设置委托(delegate)。委托(delegate)未按原样设置,因为调用 initWithFrame:andRating: 时,_ratingViewControllernil

此外,在 UIView 中引用 UIViewController 也是不好的做法。它不仅会创建一个保留循环,而且忽略了使用委托(delegate)的一半原因:增加任何符合 DelegateForSlider 的对象都可以设置为 StarRatingView 的灵 active s delegate 属性。

有关委派的更多信息,请查看 This programming overflow postApple's docs

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

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