views:

120

answers:

2

Hey there, I'm a designer thats really new to programming with xcode or Objective-C in general. I'm trying to create a few simple apps to try to get a better hang on programming the iPhone. Currently, I'm working on a very basic app which has 3 textfields, a name field and two number fields, and when you click the button, it shows in the label "name, the answer is answer" problem is, when i click the button, nothing is appearing in the label.

im pretty sure i have the code done right, i may be mistaken, i think i might have missed an outlet or something silly of the like. this is the part i get really lost on. any suggestions?

the .h:

    #import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>


@interface fordaddyAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    IBOutlet UITextField *name;
    IBOutlet UITextField *myInt1;
    IBOutlet UITextField *myInt2;
    IBOutlet UILabel *sum;
    IBOutlet UIButton *click;

}


@property (nonatomic, retain) IBOutlet UIWindow *window;


-(IBAction)click:(id)sender;



@end

the .m:

    #import "fordaddyAppDelegate.h"

@implementation fordaddyAppDelegate
@synthesize window;


- (void)applicationDidFinishLaunching:(UIApplication *)application {    


    [window makeKeyAndVisible];
}

-(IBAction)click:(id)sender;
{   
    int sum;

    sum = myInt1, myInt2;
    NSLog (name, @", the answer is %i", sum);
}

- (void)dealloc {
    [window release];
    [super dealloc];
}


@end

and im terribly sorry in advance, the preview doesnt look to pretty :/

+1  A: 

Your problem is this:

int sum;

sum = myInt1, myInt2;

sum is an integer. myInt1 and myInt2 are UITextFields. The comma does not do what you're expecting.

You need to extract the intValue from each of the textfields, and add them together using a "+" (just like you would with regular math).

Dave DeLong
Thank you for the tip! However, when i replace the comma with a + i get this error *Invalid operands to binary +* Could you possibly explain this please?
jxd215
As Dave pointed out, those are UITextFields, not ints. You can't add two text fields. You need to ask them for their `intValue`s and add those.
Chuck
+2  A: 

your problem is that you are just writting a message to the console, not displaying the result in the label...

Here's what it should be:

- (IBAction)click:(id)sender {   
    int sum = [myInt1.text intValue] + [myInt2.text intValue];
    label.text = [NSString stringWithFormat:@"%@ the answer is %d", name.text, sum];
}

That makes a variable 'sum' which is the result of the integer values of the text fields being added together. you access the string of a UITextView by using the .text. Then you have to convert it into an integer for addition, so you call the intValue method on it. You then make an NSString with stringWithFormat, and have it contain the name.text and the sum. I would highly recommend doing some more reading about Objective-C in general before you start with all this GUI stuff... just a suggestion.

micmoo