我遇到了xcode的问题。 我是对象c和xcode的菜鸟所以...请帮忙。
我有2个Viewcontrollers:ViewController (with .m/.h)
和HighScores (with .m/.h).
在HighScores中,我放了一个名为first的标签。在ViewController
中,我有UITextField
名为* textField。我希望当我输入文本时,textField中的文本在标签中,并且当已经播放的游戏的分数大于标签中已存在的文本(“第一个”)时。
所以,
这就是我 HighScore.h 的样子:
#import <UIKit/UIKit.h>
@interface HighScores: UIViewController {
IBOutlet UILabel *first;
}
@end
这是 ViewController.m :
#import "ViewController.h"
#import "HighScore.h"
...
NSString *myString = [HighScores.first];
if (score.text > myString) {
NSString *string = [textField text];
[HighScores.first setText:string]
但xcode表示当我在点'''后面输入“first”时出现错误。如果我希望xCode识别来自UIViewController
的{first}标签,我该怎么做? 1}} VewController
?
谢谢!
答案 0 :(得分:1)
在您的代码中,“first”是UILabel,它将在加载highScores视图时生成。 因为它是IBOUtlet。 其次,您正在尝试使用类名访问。 首先创建HighScore类的实例,然后尝试访问标签“first”。
#import <UIKit/UIKit.h>
@interface HighScores: UIViewController
@property (nonatomic , strong)UILabel *firstLabel ;
@end
@implementation HighScores
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle
{
self.firstLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
[self.view addSubview self.firstlabel];
}
@end
比ViewController.m
HighScore * highscoreObject = [[HighScore alloc]init];
NSString *mystring = [highscoreObject.firstLabel text];
if (score.text > mystring) {
[highscoreObject.firstLabel setText:score.text];
{
答案 1 :(得分:0)
如果您对此感到困惑,请使用通知: 在这种情况下,您还可以使用IBoutlet。 我们将使用要设置的字符串发出通知,并在HighScores中读取通知,并使用字符串send设置标签。
在ViewController.m中
if (score.text > myString) {
NSString *string = [textField text];
[[NSNotificationCenter defaultCenter] postNotificationName:@"update" object:string];
}
@interface HighScores: UIViewController
@property (nonatomic , strong) IBOutlet UILabel *firstLabel ;
@end
并在HighScores.m
@implementation HighScores
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changetext:) name:@"update" object:nil];
}
- (void) changetext:(NSNotification *)notification {
NSLog(@"Received");
self.firstLabel.text = [notification object];
}