【发布时间】: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