简体   繁体   English

按钮不起作用

[英]Button not working

Hey there, I'm a designer thats really new to programming with xcode or Objective-C in general. 嘿,我是一名设计师,对于使用xcode或Objective-C进行编程而言,我真的很陌生。 I'm trying to create a few simple apps to try to get a better hang on programming the iPhone. 我正在尝试创建一些简单的应用程序,以更好地掌握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. 当前,我正在开发一个非常基本的应用程序,它具有3个文本字段,一个名称字段和两个数字字段,并且当您单击按钮时,它会在标签“ name ,答案为答案 ”中显示问题,当我单击按钮,标签上什么也没有显示。

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: .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: .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 :/ 我非常抱歉,预览看起来并不漂亮:/

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. 这将产生一个变量“ sum”,这是文本字段的整数值相加的结果。 you access the string of a UITextView by using the .text. 您可以使用.text访问UITextView的字符串。 Then you have to convert it into an integer for addition, so you call the intValue method on it. 然后,您必须将其转换为整数以进行加法运算,因此您可以对其调用intValue方法。 You then make an NSString with stringWithFormat, and have it contain the name.text and the sum. 然后,使用stringWithFormat制作一个NSString,并使其包含name.text和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. 我强烈建议您在开始使用所有GUI东西之前,总体上阅读一些有关Objective-C的信息,这只是一个建议。

Your problem is this: 您的问题是这样的:

int sum;

sum = myInt1, myInt2;

sum is an integer. sum是一个整数。 myInt1 and myInt2 are UITextFields. myInt1myInt2是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). 您需要从每个文本字段中提取intValue ,然后使用“ +”将它们添加在一起(就像使用常规数学一样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM