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

标签 ios objective-c delegates viewcontroller uislider

我的自定义委托(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/

相关文章:

ios - 如何通过父节点进行点击?

ios - NSSearchPathForDirectoriesInDomains 和持久数据的问题

ios - 将 UIImage 转换为 8 位

ios - 后台问题中的 Apple 推送通知

iphone - 打开和关闭其他 UIViewController - 除了使用协议(protocol)和委托(delegate)之外的任何其他方法?

ios - 如何为案例 : Message sent to deallocated instance 设置委托(delegate)

ios - 单击按钮时 UITextField 不会结束编辑(委托(delegate) textFieldDidEndEditing)

ios - Xcode 6 : linker command failed with exit code 1 (use -v to see invocation) again

ios - Apple iMessage 扩展 API。检测收件人是否正在使用 iMessage

ios - IOS 应用内购买产品