【问题标题】:Custom delegate does not working in iOS自定义委托在 iOS 中不起作用
【发布时间】:2015-08-30 18:47:17
【问题描述】:

我的自定义委托不起作用。以前我使用了自定义委托,但这次它不起作用。我想将数据(更改后的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

【问题讨论】:

    标签: ios objective-c delegates viewcontroller uislider


    【解决方案1】:

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

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

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

    【讨论】:

    • 感谢您的回复。应该如何?我试过但没有用。
    • 看起来应该是self.delegate = starViewWithAnimated
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多