【问题标题】:How to import an UILabel from one UIViewcontroller to another如何将 UILabel 从一个 UIViewcontroller 导入到另一个
【发布时间】:2013-03-08 10:47:30
【问题描述】:

我遇到了 xcode 的问题。 我是 object-c 和 xcode 的菜鸟,所以......请帮忙。

我有 2 个视图控制器:ViewController (with .m/.h)HighScores (with .m/.h).

在 HighScores 中,我放置了一个名为 first 的标签。在ViewController 中,我有一个名为*textField 的UITextField。我希望 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 识别来自 HighScore UIViewController in VewController @ 的“first”标签,我该如何做到这一点987654329@?

谢谢!

【问题讨论】:

    标签: xcode uiviewcontroller uilabel textfield


    【解决方案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];
    
    {
    

    【讨论】:

    • 嗯...我怎样才能创建类的实例?你能告诉我一个代码吗?谢谢!
    • 谢谢!现在 xCode 可以识别标签,但我还有一个问题......我希望 UILabel 中的文本在分数是新的高分时被更改。我的代码看起来是正确的,但是当我运行它并播放它时,我的 UILabel 并没有改变......我对我的代码进行了一些编辑。
    • 对于你的第二个问题,如果实例相同,它将改变..两个控制器都应该处于分配状态,然后将相同的 HighScore 实例传递给另一个控制器并更改标签的文本。 . 这取决于您的应用的层次结构。
    • 好的!非常感谢!我可以投票,因为我没有 15 个声誉:S 但我将其标记为正确!
    • 我希望你也能回答你的第二个问题
    【解决方案2】:

    让我们使用通知,如果您在这里感到困惑: 在这种情况下,您也可以使用 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];
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 2015-02-10
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    相关资源
    最近更新 更多