jazz
jazz

Reputation: 232

Custom Class compiled before ViewController

I need a little help about my code. I want to create a custom bar graph. I did all the coding. When I create the objects in my custom class view it works like a charm. However, I want to make it generic. I would like to create it in ViewController. I also add a view in storyboard and change the class name to my custom class name.

It doesn't work when I try like this.

@implementation ViewController

-(void)viewDidLoad {
   [super viewDidLoad];

   GCBarGraphRow *first = [[GCBarGraphRow alloc]init];
   first.backgroundColor = [UIColor redColor];
   first.value = 20;

   GCBarGraphRow *second = [[GCBarGraphRow alloc]init];
   second.backgroundColor = [UIColor brownColor];
   second.value = 40;

   GCBarGraphColumn *container = [[GCBarGraphColumn alloc]init];
   container.values = @[first,second];

   NSLog(@"View Controller %@",container.values);
   self.myGraph.array = container;
}

But when I try to create it in my custom view class it works.

@implementation GCBarGraphView
-(id)initWithCoder:(NSCoder *)aDecoder {
   if ((self = [super initWithCoder:aDecoder])) {

      barNumber = [array.values count];
      //NSLog(@"%@",array.values);
      showDifference = NO;
      showShadow = NO;
      gradientColor = NO;
      drawBaseLine = YES;

      [self baseClassInit];

   }
   return self;
}

-(void)baseClassInit { 
   GCBarGraphRow *first = [[GCBarGraphRow alloc]init];
   first.backgroundColor = [UIColor redColor];
   first.value = 20;

   GCBarGraphRow *second = [[GCBarGraphRow alloc]init];
   second.backgroundColor = [UIColor brownColor];
   second.value = 40;

   GCBarGraphColumn *container = [[GCBarGraphColumn alloc]init];
   container.values = @[first,second];
}

Upvotes: 0

Views: 45

Answers (1)

sha
sha

Reputation: 17850

If you're creating a view in the storyboard and assign it's class to your CGBarGraphRow, you also need to create an outlet to your view controller.

Then you don't have to initialize it manually from the view controller code. Your constructor initWithCoder will be called automatically.

Update:

Try this. In the storyboard create the following: - UIView with proper frame and assign custom class to CGBarGraphView - Insert into that view two more and assign their classes to GCBarGraphRow

In your CGBarGraphView class create two outlets and bind them to the proper views in the storyboard:

@property (nonatomic, weak) IBOutlet GCBarGraphRow *row1;
@property (nonatomic, weak) IBOutlet GCBarGraphRow *row2;

Remove the code you have above for the view controller - it's not needed. Leave something like that in the CGBarGraphView code:

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

-(void)viewDidLoad {

   GCBarGraphColumn *container = [[GCBarGraphColumn alloc]init];
   container.values = @[self.row1,self.row2];

   ... whatever else you need to initialize
}

Upvotes: 1

Related Questions