iPhone Apps, Objective C, a Graph, and Functions v

2019-07-25 18:49发布

问题:

Gentleones,

I've got a UIImageView that I have in a UIView. This particular UIImageView displays a PNG of a graph. It's important for this particular view controller and another view controller to know what the graph "looks like," i.e., the function described by the PNG graph. These other view controllers need to be able to call a "function" of some sort with the X value returning the Y value.

Please ignore the fact (from an outsider's perspective) that I could probably generate the graph programmatically.

So my questions are:

1 - Where should I put this function? Should it go in one view controller or the other, or should it go in its own class or main or...?

2 - Should it be an object in some way? Or should it be a straight function? (In either case, where's it belong?)

Pardon the apparent n00b-iness of the question. It's 'cause I'm honestly a n00b! This OOP stuff is giving my head a spin having been a procedural programmer for >30 years. (And I choose to learn it by jumping into an iPhone app. Talk about baptism by fire!)

Thanks in advance,
Bill

回答1:

The graph data and the code that processes it is part of your model, not your view or your controller. Create a separate class that encapsulates the graph and associated methods and pass an instance between the controllers that need to manipulate it.