0

I have created a custom UIView (ProgressView) where I draw a shape imported via StyleKit from PaintCode.

Below are the codes. I have declared instance variable property in my custom UIView and when I try to change property from ViewController, It does not work.

ProgressView.h

#import <UIKit/UIKit.h>

@interface ProogressView : UIView

@property (nonatomic) float daysFraction;
@property (nonatomic) float pagesFraction;


@end

ProgressView.m

#import "ProgressView.h"
#import "StyleKitName.h"
#import "ViewController.h"

@implementation ProgressView
@synthesize daysFraction = _daysFraction;
@synthesize pagesFraction = _pagesFraction;


- (void)drawRect:(CGRect)rect {
    // Drawing code
    [StyleKitName drawCanvas1WithDaysFraction:self.daysFraction pageFraction:self.pagesFraction];
}


-(void)awakeFromNib {
    [super awakeFromNib];
    self.pagesFraction = 0;
    self.daysFraction = 0;
}

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

*ViewController.m**

#import "ViewController.h"
#import "ButtonAnimation.h"
#import "ProgressView.h"
#import "StyleKitName.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet ButtonAnimation *buttonView;
@property (weak, nonatomic) IBOutlet UIButton *actionButton;

@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    ProgressView *new =[[ ProgressView alloc]init];
    new.daysFraction = 0.7f; // here I am setting value to variable in custom view ProgressView but not working.

}

- (IBAction)animateTheButton:(id)sender {
    self.buttonView.layer.backgroundColor = [UIColor clearColor].CGColor;
    [self.buttonView addErrorAnimation];

}


@end
Alok
  • 119
  • 1
  • 9
  • 2
    You have created a new progress view in viewDidLoad but not added it to your view hierarchy so it won't be visible. And the reference is stored in a local variable so it will be released as soon as that need exits. If you have another instance onscreen via your storyboard screen then you should set up an IBOutlet property so that you can update *that* instance – Paulw11 Nov 01 '15 at 19:37
  • I am not 100% sure but i don't think you can use "new" as your variable name. It's possible a keyword in objective-c and if not i don't think it's a good variable name in any case :) – Yan Nov 01 '15 at 19:38
  • There is no reason that you can't set the value from an IBAction method. Perhaps show that code. – Paulw11 Nov 02 '15 at 07:17
  • I forgot to write setNeedsDisplay in button IBAction. Now it works. – Alok Nov 02 '15 at 14:54

1 Answers1

1

You need to add this view to UIViewController's view:

[self.view addSubview:progressView];

Later, you must also set a frame. Eg

[progressView setFrame:self.view.bounds];

You may do it in viewDid/WillLayoutSubviews method to change it on rotation / window resize event.

BTW, do not name your view as new, it's horrible. Such name doesn't even tell what kind of variable is it.

Nat
  • 12,032
  • 9
  • 56
  • 103