2013-03-08 91 views
0

我遇到了xcode的问题。 我是一个noob与对象C和Xcode所以...请帮助。如何从一个UIViewcontroller导入UILabel到另一个

我有2个Viewcontrollers:ViewController (with .m/.h)HighScores (with .m/.h).

在榜我已经把第一被叫标签。并在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中说,有一个当我在“点”之后键入“第一个”时发生错误。'...如果我想让xCode识别“第一个”标签f rom HighScore UIViewController in VewControllerUiViewController

谢谢!

回答

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]; 

{ 
+0

嗯...我怎样才能使一个类的实例?你能告诉我一个代码吗?谢谢! – 2013-03-09 13:32:14

+0

我做了一些编辑 – Ankit 2013-03-11 05:38:08

+0

谢谢!现在xCode识别标签,但我有另一个问题......我希望UILabel中的文本在得分为新的高分时进行更改。我的代码看起来是正确的,但是当我运行它并播放它时,我的UILabel不会更改... 我在我的代码中进行了一些编辑。 – 2013-03-11 08:03:31

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]; 
} 
+0

我不知道我在做什么错误:我是非常新的Objective-C编程...... 这里有我的项目的视频...如果你想要你可以看更多的信息:( 谢谢你或你的努力。) http://www.youtube.com/watch? v =&UG4xJFjfJdM功能= youtu.be – 2013-03-11 12:50:53

相关问题