Home > front end >  How do I use different data in the same class based on the View Controller that uses it in Objective
How do I use different data in the same class based on the View Controller that uses it in Objective

Time:11-27

I have a class that I called GaugeView, which allows me to build a "gauge" in which I display a line that colors based on the ratio between the current score (CURRENT_POINTS) and the maximum score (MAX_POINTS). enter image description here This class, at the moment is only used in one section of the app and the two variables are saved in the UserDefaults.

Now I want this class to be used in other parts of the app and I would like to use different data based on the ViewController that uses this class instead of MAX_POINTS and CURRENT_POINTS.

How can I do this? I tried to duplicate the class but Xcode gives me a compile-time error, more precisely "error 4 duplicate symbols for architecture arm64".

I thought of intervening in this part of the code:

- (void) drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
if(ctx == nil) return;
double maxPoints = [[NSUserDefaults standardUserDefaults] integerForKey:MAX_POINTS];
double currentPoints = [[NSUserDefaults standardUserDefaults] integerForKey:CURRENT_POINTS];
orangeSegmentValue = (currentPoints/maxPoints)*270.00;
[self drawBackground:rect context:ctx];
}

putting an "if", but I do not know how to set it. This is my idea:

- (void) drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
if(ctx == nil) return;
if (Im_in_ViewController_A) {
//use data from View Controller A
} else {
//do this
double maxPoints = [[NSUserDefaults standardUserDefaults] integerForKey:MAX_POINTS];
double currentPoints = [[NSUserDefaults standardUserDefaults] integerForKey:CURRENT_POINTS];
}
orangeSegmentValue = (currentPoints/maxPoints)*270.00;
[self drawBackground:rect context:ctx];
}

Any help? Thanks

CodePudding user response:

You can use a protocole to get the 2 values with a default implementation in your class :

@protocole GaugeViewDelegate {
    @required
    - (double) currentPoints();
    - (double) maxPoints();
@end

@interface GaugeView : UIView, GaugeViewDelegate
@property GaugeViewDelegate delegate;
…

@implementation GaugeView
    - (id) init() {
    …
        _delegate = self;
        ..,
     }

    - (double) currentPoints() {
         // get value from user default
        return [[NSUserDefaults standardUserDefaults] integerForKey:CURRENT_POINTS] * 1.0;
    }
    
    - (double) maxPoints() {
        return [[NSUserDefaults standardUserDefaults] integerForKey:MAX_POINTS];
    }

    - (void) drawRect:(CGRect)rect {
    CGContextRef ctx  = UIGraphicsGetCurrentContext();
if(ctx == nil) return;
        double maxPoints = [delegate maxPoints];
        double currentPoints = [delegate currentPoints];
        orangeSegmentValue = (currentPoints/maxPoints)*270.00;
        [self drawBackground:rect context:ctx];
    }
…

Then declare other view controller as implementing protocole and add the 2 methods which can be properties :

@interface oneViewVontroller : UIViewCintriller, GaugeViewDelagate 

@property double maxPoints;
@property double currentPoints;

…

@implementation oneViewVontroller

- (void) viewDidLoad() {
    …
    _maxPoints = 1000.0
           _currentPoints = 0.0;
       …

}

You could also just implement the 2 methods.

CodePudding user response:

After trying different solutions as suggested, I simply declared 2 properties in GaugeView.h

@property double maxPoints;
@property double currentPoints;

and using them into the method

- (void) drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
if(ctx == nil) return;
orangeSegmentValue = (_currentPoints/_maxPoints)*270.00;
[self drawBackground:rect context:ctx];
}

and without saving them into NSUserDefaults, so I can assign them values whenever I need into classes that uses GaugeView.

  • Related