Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

wbeck

macrumors newbie
Original poster
Feb 23, 2012
3
0
OK I am new to this. This is my first app so please be nice... I am working on an app to convert units. Yes I know they are out there but I want to learn. The problem I am running into is getting the view to redraw once I click out of the text box. I dont want to have to click a calculate button.

Code:
//
//  ViewController.m
//  MedConversion
//
//  Created by Wayne Beck on 2/23/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end
@implementation ViewController

- (IBAction)reset {
    1.text = @"0";
    2.text = @"0";
    3.text = @"0";
    4.text = @"0";
    5.text = @"0";
    6.text = @"0";
    7.text = @"0";
    8.text = @"0";
    9.text = @"0";
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    UITouch *touch = [[event allTouches] anyObject];
    if (touch.tapCount >= 1) {
        [1 resignFirstResponder];
        [2 resignFirstResponder];
        [3 resignFirstResponder];
        [4 resignFirstResponder];
        [5 resignFirstResponder];
        [6 resignFirstResponder];
        [7 resignFirstResponder];
        [8 resignFirstResponder];
        [9 resignFirstResponder];
    }
}

-(IBAction) resignResponder {
    [1 resignFirstResponder];
    [2 resignFirstResponder];
    [3 resignFirstResponder];
    [4 resignFirstResponder];
    [5 resignFirstResponder];
    [6 resignFirstResponder];
    [7 resignFirstResponder];
    [8 resignFirstResponder];
    [9 resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField == 1) {
        [1 resignFirstResponder];
        
        double c = (5.0/9.0)*([1.text doubleValue]-32.0);
        double k = c - 273.15;
        
        2.text = [[NSString alloc]initWithFormat:@"%2.1f", c];
        3.text = [[NSString alloc]initWithFormat:@"%2.1f", k];
    }
    if (textField == 2) {
        [2 resignFirstResponder];
        
        double f = (9.0/5.0*[2.text doubleValue])+32.0;
        double k = [2.text doubleValue] - 273.15;
        
        1.text = [[NSString alloc]initWithFormat:@"%2.1f", f];
        3.text = [[NSString alloc]initWithFormat:@"%2.1f", k];
    }
    if (textField == 3) {
        [3 resignFirstResponder];
        
        double c = [3.text doubleValue] + 273.15;
        double f = (9.0/5.0*c)+32.0;
        
        2.text = [[NSString alloc]initWithFormat:@"%2.1f", c];
        1.text = [[NSString alloc]initWithFormat:@"%2.1f", f];
    }
    return YES;
}


/*- (void)drawRect:(CGRect)rect {
    // Drawing code
}*/


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end
 
Last edited by a moderator:

MattInOz

macrumors 68030
Jan 19, 2006
2,760
0
Sydney
The thing I noticed first pass was a lot of -resignFirstResponder calls.
As I understand it there is only one firstResponder and the system knows which view is it, so you should only need to tell one view to -becomeFirstResponder
 
Last edited by a moderator:

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
I feel confident that integers are not valid identifiers in C or Objective-C. Can you name a variable 0?

It is customary when posting here to ask a question. What's yours?

It would be contrary to good sense to add a drawRect: method to a view controller.

It's not so common these days to use the touches methods. UIGestureRecognizers are more commonly used for these things. Also, if you do implement one of the four touches methods it's recommended that you implement all four.
 
Last edited by a moderator:

wbeck

macrumors newbie
Original poster
Feb 23, 2012
3
0
redraw view is what i am trying to do...

I am trying to figure out how a sample app i downloaded works. to develop mine. in the sample you enter a value in a text box and to other text boxes update as soon as you click on the background. The sample I am looking at had all the firstresponders in it. Maybe I will back up and punt and start again.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
I am trying to figure out how a sample app i downloaded works...

When you reference an external resource, it's courteous to provide specifics about it. If it's a book, title, author, edition. Online tutorial? Provide a link. So, you should also let us know where this sample app came from.
 

wbeck

macrumors newbie
Original poster
Feb 23, 2012
3
0
sample came from http://appsamuck.com/

I am trying to recreate the temp convert app. I have looked and done tutorials of conversion apps that require a button to be pressed. I like this one as it does not.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
OK, I downloaded the TemperatureConverter project from http://appsamuck.com/day20.html

The Base SDK was 2.0 so this is a little old but it does still compile and work correctly. The code has some obvious bugs (memory leaks that the analyzer will tell you about) and an odd design where it puts some code in a view that would usually go into a view controller. It creates the view so that tapping the view can make the keyboard go away and lets the app do the calculations. It does a few other things that are odd because it's a utility app with main and flip side view controllers. It's based on the Utility app template that was part of Xcode at that time but this stuff is done differently now. Anyway it's not the best example in the world but probably not the worst either.

The basic idea of how the app works is OK. There are several UITextFields that represent temperatures in different scales. When the user hits the return key the appropriate delegate method is called and the app calculates the results. Also if the user taps anywhere on the screen the touchesBegan method is called and it hides the keyboard.

You might want to set breakpoints in the code and then run the app to see what happens.

Your attempt to build something similar to this should work. You just need to use variable names that are valid. textfield1, textfield2 etc.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.