Question: iOS Charts - How to initialize LineChartView programmatically


iOS Charts - How to initialize LineChartView programmatically

Answers 1
Added at 2017-09-06 21:09

I want to use iOS charts for my objective-c project. Since the UI is written in code completely, I don't want to create a nib file for the chart view specifically. However, a simple init or initWithFrame to create the LineChartView is giving me nil

//Declare chartView property in header
@property (nonatomic, weak) LineChartView* chartView;

//Call to init chart     
CGRect frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 
self.chartView = [[LineChartView alloc] initWithFrame: frame];

Here, self.chartView is nil after calling above code.

Answers to

iOS Charts - How to initialize LineChartView programmatically

nr: #1 dodano: 2017-09-07 11:09

As per my exprience you need to remove Weak property only nonatomic will work while you are assigning object with Init method.

@property (nonatomic, weak) LineChartView *lineChart;

This one should replaced with

@property (nonatomic) LineChartView *lineChart;

as if you create weak property it will release after its assignment.

also while you make this type of mistake XCode throws warning like below :

warning: assigning retained object to weak property; object will be released after assignment [-Warc-unsafe-retained-assign] self.lineChart = [[LineChartView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)]; ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1 warning generated.

So in-sort never use weak while you are assigning any retain object in it.

Hope this will help!

Source Show
◀ Wstecz