2013-03-08 36 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

相關問題