Assignment #3 Task #3

Please note, this blog entry is from a previous course. You might want to check out the current one.

Add a Graph button to your calculator’s user-interface that, when pressed, segues to a new MVC (that you will have to write) by pushing it onto the UINavigationController’s stack (on the iPhone). The new MVC graphs whatever program was in the calculator when the button was pressed. To draw the graph you will iterate over all the pixels in your view horizontally (x) and use +runProgram:usingVariableValues: to get the corresponding vertical (y) value. You will, of course, have to convert to/from your view’s coordinate system from/to a reasonable graph coordinate system. You will need a scale and origin to do this coordinate system conversion. If the user has not already chosen a scale and origin for the graph (see Required Tasks 7 & 8 below), pick a reasonable starting scale and origin.

… and still inside your storyboard, drag out a new view controller, place a new button called “Graph” into your calculator view controller and create a push segue from your new button to your new view controller.

To create a new MVC for the new controller click File -> New -> File… -> Objective-C class -> Next. Name it something like GraphViewController and subclass it from UIViewController.

To be able to runProgram, we need an actual program as public property, where es scale and origin could also be private:

@property (nonatomic, strong) CalculatorBrain *program;
@property (nonatomic) CGFloat scale;
@property (nonatomic) CGPoint origin;

Don’t forget to synthesize them. For scale we use lazy instantiation, for origin a start value of (0,0) is sufficient for now.

- (CGFloat)scale 
{
    if (!_scale) return DEFAULT_NUMBER_OF_POINTS_PER_UNIT;
    return _scale;
}

As the actual drawing will happen in task #5, we will just use some logging to see if our code actually works.

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"program: %@", self.program);
    NSLog(@"scale: %g", self.scale);
    NSLog(@"origin: %g %g", self.origin.x, self.origin.y);
}

When you change now the class of the newly created controller in storyboard to GraphViewController and run the app it should show origin as (0,0), scale as 1 and program as (null) as we have not yet assigned it to anything.

Thus we name the segue in storyboard “ShowGraph” and adjust CalculatorViewController.m:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"ShowGraph"]) {
        if (self.userIsInTheMiddleOfEnteringANumber) [self enterPressed];
        [segue.destinationViewController setProgram:self.brain.program];
    }        
}
FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

2 thoughts on “Assignment #3 Task #3”

  1. do i type those, @property (nonatomic, strong) CalculatorBrain *program;
    @property (nonatomic) CGFloat scale;
    @property (nonatomic) CGPoint origin;
    in the graphView.f file ?

    1. Normally you put property declarations into your header file (.h) if you want them to be public, so that other portions of your code can access them, or if you want to subclass your class and access your properties from there. If you want to have your properties private, in order to access them only from the current class, you put the declarations into your .m file.

Leave a Reply

Your email address will not be published.