自定义委托在 iOS 中不起作用

Custom delegate does not working in iOS

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

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

并且在这个 class 中我想获得那个 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 方法中设置委托。委托未按原样设置,因为调用 initWithFrame:andRating: 时,_ratingViewControllernil.

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

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

_ratingViewController.delegate = self; ratingsViewController 没有记忆它将委托设置为该协议的自身。 试试这个

_ratingsViewController = [[UIViewController alloc]init];
_ratingViewController.delegate = self;