从InterfaceBuilder内部调用一个方法声明了UIView的子类

时间:2018-04-09 13:03:04

标签: objective-c xcode methods

所以我在访问UISubview类中的方法时遇到了麻烦。我没有通过代码进行初始化,但我添加了一个UIView并将类设置为IVVerticalProgress

但是当我试着打电话时

[_weakIBOutlet setProgressValue:50];

什么都没发生,我没有打印。但是,如果我初始化对象它完全正常。但是没有更新视图。

这里有什么帮助吗? : - )

标题

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@class IVVerticalProgress;

IB_DESIGNABLE

@interface IVVerticalProgress : UIView {
    UIView *gauge;
    CGFloat initialHeight;
}

@property (nonatomic) IBInspectable CGFloat cornerRadius, borderWidth;
@property (nonatomic) IBInspectable UIColor *fillColor, *borderColor;
@property (nonatomic) IBInspectable double animationDuration;

-(void)setProgressValue:(CGFloat)value;

@end

车身

#import "IVVerticalProgress.h"

@implementation IVVerticalProgress

-(void)drawRect:(CGRect)rect {
    self.layer.backgroundColor = self.fillColor.CGColor;
    self.layer.borderColor = self.borderColor.CGColor;
    self.layer.borderWidth = self.borderWidth;
    self.layer.cornerRadius = self.cornerRadius;
    self.clipsToBounds = YES;

    gauge = [[UIView alloc] initWithFrame:CGRectMake(0,0,rect.size.width,0)];
    [gauge setBackgroundColor:[UIColor redColor]];
    [self addSubview:gauge];
}

- (id)sharedInit {
    self.backgroundColor = [UIColor clearColor];
    self.layer.transform = CATransform3DMakeRotation(M_PI, 0.0, 0.0, 1.0);
    initialHeight = self.layer.frame.size.height;
    return self;
}


- (id)initWithFrame: (CGRect)frame {
    self = [super initWithFrame: frame];
    if (self) {
        self = [self sharedInit];
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self = [self sharedInit];
    }
    return self;
}

-(void)setProgressValue:(CGFloat)value {
    if (value < 0)
        value = 0;

    if (value > 100)
        value = 100;

    CGFloat percentageHeight = (initialHeight / 100) * value;
    [gauge setFrame:CGRectMake(gauge.frame.origin.x,
                               gauge.frame.origin.y,
                               gauge.frame.size.width,
                               percentageHeight)];
    NSLog(@"%f",value);
}

@end

1 个答案:

答案 0 :(得分:0)

在这两种情况下,如果你在故事板中使用拖放或使用initWithframe,它将很好地工作,因为你已经实现了initWithCoder和initWithFrame,你的setProgressValue与你创建视图的方式无关

这里是使用您的IVVerticalProgress的项目链接,您可以检查是否已经以编程方式和故事板使用,并且两者都能正常工作。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [_temp setProgressValue:50];

    IVVerticalProgress *fromCode = [[IVVerticalProgress alloc]initWithFrame:CGRectMake(20,300, 200, 200)];
    [fromCode setFillColor:UIColor.blueColor];
    [fromCode setBorderColor:UIColor.cyanColor];
    [fromCode setCornerRadius:10];
    [fromCode setBorderWidth:10];
    [fromCode setProgressValue:100];
    [self.view addSubview:fromCode];
}

OutPut 
2018-04-10 19:55:24.482438+0530 testView[4011:134919] 50.000000
2018-04-10 19:55:24.482755+0530 testView[4011:134919] 100.000000

OutPut in Simulator

如果仍然不能为你工作,请告诉我们: - )