Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 23, 2012, 06:09 PM   #1
wbeck
macrumors newbie
 
Join Date: Feb 2012
view redraw....

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 dejo; Feb 23, 2012 at 11:44 PM. Reason: Please use [code] tags.
wbeck is offline   0 Reply With Quote
Old Feb 23, 2012, 07:08 PM   #2
robvas
macrumors 65816
 
Join Date: Mar 2009
Location: USA
So you're naming your variables numerals?
robvas is offline   0 Reply With Quote
Old Feb 23, 2012, 07:16 PM   #3
MattInOz
macrumors 68020
 
MattInOz's Avatar
 
Join Date: Jan 2006
Location: 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
__________________
There is no such thing as "Collective Wisdom"
[13" MacBookPro 2.7Ghz, 27"Al iMac i7, Black MacBook 13", iPhone 4, iPad]

Last edited by dejo; Feb 23, 2012 at 11:45 PM. Reason: Code tags added to referenced post.
MattInOz is offline   0 Reply With Quote
Old Feb 23, 2012, 10:31 PM   #4
PhoneyDeveloper
macrumors 68020
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
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 dejo; Feb 23, 2012 at 11:46 PM. Reason: Code tags added to referenced post.
PhoneyDeveloper is offline   0 Reply With Quote
Old Feb 24, 2012, 09:27 PM   #5
wbeck
Thread Starter
macrumors newbie
 
Join Date: Feb 2012
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.
wbeck is offline   0 Reply With Quote
Old Feb 24, 2012, 11:01 PM   #6
dejo
Demi-God (Moderator)
 
dejo's Avatar
 
Join Date: Sep 2004
Location: Colorado
Quote:
Originally Posted by wbeck View Post
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.
__________________
My iOS Apps: a.k.a., DreamStream
I support the MacRumors Blood Drive!
dejo is offline   0 Reply With Quote
Old Feb 25, 2012, 10:07 AM   #7
wbeck
Thread Starter
macrumors newbie
 
Join Date: Feb 2012
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.
wbeck is offline   0 Reply With Quote
Old Feb 25, 2012, 11:13 AM   #8
dejo
Demi-God (Moderator)
 
dejo's Avatar
 
Join Date: Sep 2004
Location: Colorado
Quote:
Originally Posted by wbeck View Post
sample came from http://appsamuck.com/
Please provide the exact URL.
__________________
My iOS Apps: a.k.a., DreamStream
I support the MacRumors Blood Drive!
dejo is offline   0 Reply With Quote
Old Feb 25, 2012, 03:30 PM   #9
PhoneyDeveloper
macrumors 68020
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
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.
PhoneyDeveloper is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Conditionally display views on button press johnmerlino Mac Programming 4 Nov 13, 2011 09:11 AM
Setting Default Arrange/Sort Views (Finder) defected07 Mac OS X 10.7 Lion 1 Aug 6, 2011 06:00 PM
Resolved: Loading a Progmatically created view Controller LastLine iPhone/iPad Programming 3 Apr 27, 2011 09:25 PM
How would I access a label on another view controller? KiranPanesar iPhone/iPad Programming 11 Apr 19, 2011 10:15 AM
some help on Editable Detail views kingthong iPhone/iPad Programming 8 Mar 21, 2011 04:56 AM


All times are GMT -5. The time now is 08:59 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

Privacy / DMCA contact / Affiliate and FTC Disclosure
Copyright 2002-2013, MacRumors.com, LLC