【问题标题】:Semantic Issue with UITextfieldUITextfield 的语义问题
【发布时间】:2013-05-20 06:06:21
【问题描述】:

我是 iOS 新手。我在这里搜索了 UITextfield Sematic Issues,以及它的变体。我正在尝试制作一个非常简单的应用程序,你:

在文本字段中输入文本 按按钮 它将标签更改为在文本字段中输入的文本。

但我不断收到这个奇怪的语义错误,我不知道如何解决它。

代码在这里:

.m 文件。

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *myTextField;


@property (weak, nonatomic) IBOutlet UILabel *myLabel;



- (IBAction)changeLabel:(id)sender;

@end

.h

#import "ViewController.h"

@interface ViewController ()


@end
@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)changeLabel:(id)sender {
    NSString *message = [[NSString alloc] initWithFormat:@"Hello %@", [myTextField text]];
    [myLabel setText:message];
}

谢谢。

【问题讨论】:

  • 您遇到了什么编译器错误?请张贴。
  • 我实现了你的代码,它工作正常.....你的错误是什么
  • @AhmedZ。你的说法不正确。 Objective C 允许使用这种语法访问属性和方法。

标签: ios compiler-errors uilabel semantics


【解决方案1】:

问题是您直接引用您的属性而没有“自我”。用这个替换你的 m 文件内容:

@interface ViewController ()


@end
@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)changeLabel:(id)sender {
    NSString *message = [[NSString alloc] initWithFormat:@"Hello %@", [self.myTextField text]];
    [self.myLabel setText:message];
}

@end

【讨论】:

  • 你的解决方案也对,我需要参考自我。感谢您的帮助。
【解决方案2】:

我期待像 unknown Reciever 'myTextField' 这样的错误,这意味着,

在 IOS 6 苹果中默认提供 Synthesize 属性,因此您可以使用

要么

NSString *message = [[NSString alloc] initWithFormat:@"Hello %@", [_myTextField text]];

NSString *message = [[NSString alloc] initWithFormat:@"Hello %@", [self.myTextField text]];

【讨论】:

  • 这解决了我的问题。非常感谢,我是在前面没有 self 的情况下引用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-29
  • 1970-01-01
  • 2012-11-22
  • 1970-01-01
相关资源
最近更新 更多