Simple hello world app problem.

Discussion in 'iOS Programming' started by RobertD63, Apr 18, 2010.

  1. RobertD63 macrumors 6502

    Joined:
    Feb 17, 2008
    Location:
    A place
    #1
    I've read (and viewed) the whole video and book of "Programming in Obj-c 2.0". Im trying to build a simple little program to increase the label +1 in a number when tapped increased and the opposite when you decrease. Everything is set up Im just trying to figure out what to put into the @implementation side. Heres what I have:
    Code:
    @implementation AppDelegate_Phone
    
    @synthesize window,view,increaseButton,decreaseButton,numberOfSidesChange;
    
    -(IBAction)increase {
    	
    	
    }
    
    -(IBAction)decrease {
    	
    	
    }
    
    //More code.....
    
    Code:
    @interface AppDelegate_Phone : NSObject <UIApplicationDelegate> {
        UIWindow *window;
    	UIView *view;
    	IBOutlet UIButton *decreaseButton;
    	IBOutlet UIButton *increaseButton;
    	IBOutlet UILabel *numberOfSidesChange;
    }
    @property (nonatomic, retain) IBOutlet UIWindow *window;
    @property (nonatomic, retain) IBOutlet UIView *view;
    @property (nonatomic, retain) IBOutlet UIButton *increaseButton;
    @property (nonatomic, retain) IBOutlet UIButton *decreaseButton;
    @property (nonatomic, retain) IBOutlet UILabel *numberOfSidesChange;
    
    -(IBAction)increase;
    -(IBAction)decrease;
    
    @end
    
    So what would I use?
     
  2. PatrickCocoa macrumors 6502a

    Joined:
    Dec 2, 2008
    #2
    Code:
    -(IBAction)increase {
    	numberOfSidesChange.text = numberOfSidesChange.text + 1;
    	
    }
    
    -(IBAction)decrease {
    	numberOfSidesChange.text = numberOfSidesChange.text - 1;
    	
    }
    
    except that you'll need to convert numberOfSidesChange.text to an int on the right hand side of the assignment, then do the add or subtract, then convert back to text.

    I'm too lazy to look at my old code, but I've done it several times.
     
  3. RobertD63 thread starter macrumors 6502

    Joined:
    Feb 17, 2008
    Location:
    A place
    #3
    That seems a little more complicated then what I've been playing with here:
    Code:
    @implementation AppDelegate_Phone
    
    @synthesize window,view,increaseButton,decreaseButton,numberOfSidesChange;
    int number;
    
    -(IBAction)increase {
    number = number+1;	
    numberOfSidesChange.text=number;	
    }
    
    -(IBAction)decrease {
    number = number-1;
    numberOfSidesChange.text=number;
    	
    }
    
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    How is number defined?

    Besides, it's gonna have to be somewhat complicated because you are needing to convert a number to an NSString, in a manner of speaking, since that's what class type a UILabel's text property is.
     
  5. RobertD63 thread starter macrumors 6502

    Joined:
    Feb 17, 2008
    Location:
    A place
    #5
    Oh, so its not so simple after all. Well for me. Just wondering if I chose the wrong kind of thing to get my feet wet with iPhone apps etc?

    At any rate im still interested in finishing this. How would I convert it to an NSString? If I did would I have to convert it back to an integer?
     
  6. ChOas macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #6
    Well, how about you use an int to to represent the number of sides and do your calculations on. Only convert it to NSString (hint: look for StringWithFormat: ) to display it... no need to convert backwards and forwards.
     
  7. RobertD63 thread starter macrumors 6502

    Joined:
    Feb 17, 2008
    Location:
    A place
    #7
    Ah hah! Got it!
    Code:
    -(IBAction)decrease {
    	number=number-1;
    	numberOfSidesChange.text = [NSString stringWithFormat:@"%d", number];
    
    }
     

Share This Page