【问题标题】:button or label not working按钮或标签不起作用
【发布时间】:2013-10-12 12:55:56
【问题描述】:

当我尝试在单击按钮时更改标签的文本时,代码会编译但它不起作用。我单击按钮,没有任何反应。 目前,我只是试图在按下按钮solveButton时在标签x1Label上显示“错误”。你能帮我修一下吗?

.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

谢谢

【问题讨论】:

  • 像这样在每个对象前面写下 self :- "self.aField"
  • 正确检查您的 IBOutlet 并尝试使用 self.errorLabel.text=@"error";
  • self.aField..应该在哪里写,应该怎么做?

标签: ios uibutton uilabel


【解决方案1】:
  1. 您在 errorLabel 上而不是在 x1Label 上书写。

  2. 您是否将 SolveButton 操作连接到情节提要中的按钮? 如果没有转到情节提要 > 单击您的按钮 > 转到连接检查器(在右侧面板上)并将操作链接到您的按钮。

【讨论】:

  • 感谢您的快速响应。它是否进入错误标签或 x1 标签并不重要,因为我只想让它工作。此外,我似乎无法在我的右侧面板上找到连接检查器。但是我的按钮在引用连接到“视图控制器”的插座中确实有我的按钮
  • 您需要将操作连接到您的按钮,否则他不知道您希望何时完成操作。连接检查器位于您单击情节提要时看到的右上角面板中。有许多图标,您正在寻找的是指向屏幕外的箭头(右侧的最后一个)。如果你仍然找不到它谷歌它并查看图片:)
  • 如果您的 SolveButton 连接到按钮,您的意思是按下它很好!还是不行?如果是这样,代码中一定有一些错误,我会检查它。
  • 试试这个:self.errorLabel.text = @"Error";或 _errorLabel.text = @"错误";
  • 非常感谢你们。感谢您的帮助,我按照 ObiWanKeNerd 的建议打开了故事板,然后将一个箭头从按钮拖到了我的 .a (IBAction) 代码中。现在它起作用了。谢谢!!! (由于是新用户,无法回答我自己的问题)
猜你喜欢
  • 2018-11-09
  • 2021-09-29
  • 2017-04-26
  • 2022-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-22
  • 1970-01-01
相关资源
最近更新 更多