Connected to aSimple Calculator Based on iOS (1)These are the experimental contents of the PO main iOS course.

The new interface

  1. Advanced calculator: can perform some advanced operations such as trigonometry, factorial, logarithm, etc.

  2. Fitness Calculator: After obtaining the user’s height and weight, the user’s body mass index is fed back.

Functions that need to be done

  1. Multi-scene switching

  2. Advanced calculator

  3. Fitness calculator

  4. Equipment lock screen

  5. Draw: Adds a rectangular border to the output in the fitness calculator

  6. Controls the length of the string displayed on the screen

Multi-scene switching

  • Scenario: In the storyboard, a scene refers to a single view controller and a view

  • Scene transitions: Manage transitions between two scenes and manage transitions between two scenes.

  1. Drag a ViewController

  2. Create a new UIViewController class, SecondViewController, and connect it with the previous ViewController

  3. In order to communicate between the two view controllers, instances of classes that are set to be used for advanced computing should be placed in the declaration // Private in.m files

  4. Set an attribute, Screen, to communicate between view controllers

  5. Add a Navigation Controller and establish a connection between the Navigation Controller and the two view controllers you already have. Note: With the navigation controller for scene transitions, only Push can be selected

  6. Note: When the segue initiates, there are a few things to do: // For data transfer

    • I’m gonna select a segue, I’m gonna name it // and there might be multiple scenes that switch, and I need to figure out which one it is

    • Overwrite the PrepareForSegue: method in the first scenario. If the scene transitions to the advancedCalculator view, pass the AdvancedCalculator instance object from the first scene to the second scene

      - (void) prepareForSegue segue: (UIStoryboardSegue *) sender: (id) sender {/ / pass the second scenario if the instance of the advanced object ([segue. Identifier IsEqualToString: @ "SecondScene"]) {/ / judge whether the second scenario if ([segue. DestinationViewController isKindOfClass: [SecondViewController Class]]) {/ / judgment objective view controller SecondViewController * SVC = (SecondViewController *) segue. DestinationViewController; svc.calculator = self.calculator; // pass only the content}}}
  7. In both view controllers, you want to display the results on screen, so in the viewWillAppear method of both view controllers, you add the following code

    self.inputText.text = self.calculator.screen; // Passed back from another scene
  8. Note that after each calculation, you change both the value that is printed on the screen and the value of the screen attribute that is passed

Advanced calculator

  1. Add an advanced computing interface

  2. Add a Model for advanced computing. Since you can use the calculated class you wrote earlier, the new class inherits from that class, and then just adds some more methods for advanced calculations. There are many ways to achieve advanced computation. Here we use functions in the C language math library

  3. Buttons like parentheses, e, PI, and so on, because we need to get the value of the button to display on the result, we need to set up an outlet connection

  4. How to identify a button and execute its corresponding method? There are many ways to do this: you can add an Action to each button, or you can add only one Action, but you can add a tag value to the buttons to identify the different buttons. For example, if you add only one action, set the abs button tag to 4, and when the button is pressed, if the tag = 4 is detected, then execute the following code.

       if (sender.tag == 4) {
           self.inputText.text = [self.calculator abs:self.calculator.input];        
           NSMutableString *tempStr = [NSMutableString stringWithString:self.inputText.text];
           self.calculator.input = tempStr;
           self.calculator.screen = tempStr;
       }
    
  5. When a button is pressed, the corresponding method in the model is called.

Fitness calculator

  1. Add an interface, drag a view controller, and add the input field of height and weight to get the user’s height and weight value for constitution calculation

  2. Create a new UIViewController class to associate with the view controller in 1

  3. Create the corresponding Model, which can inherit from the Calculate class or use Categories. The method of class is used here. The class is not a new class, but only extends some methods on the basis of the original class. Therefore, only methods can be added to the class, but no attributes can be added. // The main idea here is to use MVC, separating the business logic from the view

    -(NSString *)computeHealthBMI:(NSString *)height weight:(NSString *)weight { NSMutableString *expression; expression = [NSMutableString stringWithString:weight]; [expression appendString:@"/(("]; [expression appendString:height]; [expression appendString: @ "/ 100.0)"]. [expression appendString:@"*("]; [expression appendString:height]; [expression appendString: @ "/ 100.0)) ="]. self.input = expression; NSLog(@"health compute, input is %@", self.input); NSLog(@"health compute, expression is %@", expression); return [NSString stringWithFormat:@"%.2f", [[self ExpressionCalculate:self.input] floatValue]]; }
  4. Notice that I added an equal sign at the end because I used the equal sign for symbol identification in the calculation method I wrote earlier in the Calculate class.

Equipment lock screen

The device lock screen can be coded, but I struggled to figure it out, and then I turned it off in General

Add a rectangle border

So this is basically about drawing on UIView, two problems that you have to solve, how to draw a rectangle and how to draw on a View.

  1. First, create a new class that inherits UIView, such as DrawRect, and override the DrawRect method in the class.

Then, draw the rectangle in the drawRect; Finally, bind the drawing view controller to the newly created DrawRectView.

  1. The code for drawing the rectangle was found by others and has not been studied in detail. The code for drawing the rectangle to be completed on January 11, 2015 refers to the content in the course of Uncle White Beard at Stanford, which is in Lesson 7. Factor is defined so that no matter how our view changes, we can draw a picture in the same scale and position on the view. Self. Bounds is the rectangle that you use when your own view is drawn in your own code. The specific assignment of the white-bearded uncle will be detailed next time. (Po is another pile of dead line pressure (╥ Haze)

#define DEFAULT_FACE_CARD_SCALE_FACTOR 0.90
#define CORNER_FONT_STANDARD_HEIGHT 180.0
#define CORNER_RADIUS 12
  • (CGFloat)cornerScaleFacctor { return self.bounds.size.height / CORNER_FONT_STANDARD_HEIGHT; }

  • (CGFloat)cornerRadius { return CORNER_RADIUS * [self cornerScaleFacctor]; }

  • (cgFloat)cornerOffset {return [self cornerRadius] / 3.0; }

    -(void)drawRect:(CGRect)rect{

    UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:[self cornerRadius]];
    [roundedRect addClip];
    [[UIColor whiteColor] setStroke];   
    [roundedRect stroke];   

    }

  • (void)setup

    {

    self.backgroundColor = nil; //don't draw background self.opaque = NO; self.contentMode = UIViewContentModeRedraw; //bounds change call drawRect

    }

  • (void)awakeFromNib

    {

    [self setup];
    [self setNeedsDisplay];   

    }

Control screen length

// I feel my way is not elegant…

  1. The output on the screen is changed by pressing a button, so it should be changed in the action method of the TouchButton.

  2. In addition, the output of the screen is stored in inputText.text, but the previous code used inputText.text to assign the value. To control the output length of the screen, you should avoid using this assignment.

  3. Finally, I used a magic number, observed that the length of 13 is not automatically reduced (in fact, it seems to be able to modify the TextField property to control this), and then divided it into two cases.

    If ([[sender titleLabel] text] isEqualToString:@”×”]) {

       [self.calculator.input appendString:@"*"];
       

    }else if([[sender titleLabel] text] isEqualToString:@”÷”]){

       [self.calculator.input appendString:@"/"];

    }else

       [self.calculator.input appendString:[[sender titleLabel] text]];
    

    If (self) inputText) text. Length = = 13) {/ / screen length control at 13

       NSMutableString *str = [NSMutableString stringWithString:self.calculator.input];
       NSLog(@"inputtxt=13, str is %@", str);
       self.calculator.screen = str;
       self.inputText.text = [str substringWithRange:NSMakeRange(str.length - 13, 13)];

    }else{// The screen length does not exceed 13

       NSMutableString *str = [NSMutableString stringWithString:self.inputText.text];
       [str appendString:[[sender titleLabel] text]];
       NSLog(@"inputtxt<13, str is %@", str);
       self.calculator.screen = str;
       self.inputText.text = str;

    }

rendering

The complete code