【问题标题】:calling a method from inside InterfaceBuilder declared UIView subclass从 InterfaceBuilder 内部调用方法声明 UIView 子类
【发布时间】:2018-09-18 21:27:39
【问题描述】:

所以我无法访问 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

【问题讨论】:

  • 您是否再次检查了它的调用并为我打印了价值

标签: objective-c xcode methods


【解决方案1】:

在这两种情况下,如果您在情节提要中使用拖放或使用 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

如果仍然不适合您,请告诉我们:-)

【讨论】:

  • 好的,所以在尝试了所有这些之后。我意识到我的 IBOutlets 连接不正确。因此,感谢您的广泛解释,并感谢您抽出宝贵的时间。既然您以正确的方式引导我,我将接受您的答案为正确的答案! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多