当我尝试在按钮单击时更改标签的文本时,代码会编译但不起作用。我点击按钮,没有任何反应。 目前,我只是在按下按钮solveButton时尝试在标签x1Label上显示“Error”。你能帮我解决一下吗?
·H
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize errorLabel;
@synthesize aField, bField, cField;
@synthesize x1Label, x2Label;
@synthesize solveButton;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
aField.delegate = self;
bField.delegate = self;
cField.delegate = self;
aField.keyboardType = UIKeyboardTypeNumberPad;
bField.keyboardType = UIKeyboardTypeNumberPad;
cField.keyboardType = UIKeyboardTypeNumberPad;
NSString *str = @"car";
errorLabel.text = str;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[aField resignFirstResponder];
[bField resignFirstResponder];
[cField resignFirstResponder];
return YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[aField resignFirstResponder];
[bField resignFirstResponder];
[cField resignFirstResponder];
}
- (IBAction)solveButton:(id)sender
{
errorLabel.text = @"Error";
}
@end
的.m
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITextFieldDelegate>
{
IBOutlet UITextField *aField, *bField, *cField;
}
@property (weak, nonatomic) IBOutlet UILabel *errorLabel;
@property (strong, nonatomic) IBOutlet UITextField *aField;
@property (strong, nonatomic) IBOutlet UITextField *bField;
@property (strong, nonatomic) IBOutlet UITextField *cField;
@property (weak, nonatomic) IBOutlet UILabel *x1Label;
@property (weak, nonatomic) IBOutlet UILabel *x2Label;
@property (weak, nonatomic) IBOutlet UIButton *solveButton;
@end
谢谢
答案 0 :(得分:0)
您正在写错误标签而不是x1Label。
您是否将SolveButton操作连接到故事板中的按钮? 如果没有去故事板&gt;点击你的按钮&gt;转到Connection Inspector(在右侧面板上)并将操作链接到您的按钮。