在另一个类中设置标签文本

发布于 2025-01-06 15:09:51 字数 981 浏览 4 评论 0原文

在我的视图控制器中, 我在这里创建一个标签。

在另一个视图控制器中,我正在创建第一个视图控制器的实例。这里我尝试设置标签的文本属性。但这不起作用。我一遍又一遍地检查我的代码,没有发现任何遗漏的东西。 我尝试以编程方式以及使用界面生成器创建标签。仍然无法设置标签的文本属性。这有什么原因吗?

中创建标签

- (void)viewDidLoad
{
myLabel = [[UILabel alloc]init];
[myLabel setText:@"Hi"];
[myLabel1 setText:@"Hello"];
 [myLabel setFrame:CGRectMake(105, 130, 120, 30)];
 [myLabel setBackgroundColor:[UIColor clearColor]];
 [myLabel setTextColor:[UIColor whiteColor]];
 [myLabel setTextAlignment:UITextAlignmentCenter];
 [myLabel setAdjustsFontSizeToFitWidth:YES];
 [self.view addSubview:myLabel];
 [myLabel release];
}

我在我的第一个视图控制器

MySecondViewcontroller *showMyViewcontroller = [[ MySecondViewcontroller    alloc]initWithNibName:@"MySecondViewcontroller" bundle:nil];;
showMyViewcontroller.myLabel = @"I cant set the text";
[self.navigationcontroller pushViewController: showMyViewcontroller animated:YES];
[showMyViewcontroller release];

我在这里做错了什么

In my viewcontroller,
I am creating a label here.

In another viewcontroller I am creating an instance of the first view controller. Here I tried setting the text property of the label. But it doesn't work. I have checked my code over and over and couldn't find any anything missing.
I tried creating the label both programmatically as well as using interface builder. Still cant set the text property of the label. Any reason for this?

I create the label in

- (void)viewDidLoad
{
myLabel = [[UILabel alloc]init];
[myLabel setText:@"Hi"];
[myLabel1 setText:@"Hello"];
 [myLabel setFrame:CGRectMake(105, 130, 120, 30)];
 [myLabel setBackgroundColor:[UIColor clearColor]];
 [myLabel setTextColor:[UIColor whiteColor]];
 [myLabel setTextAlignment:UITextAlignmentCenter];
 [myLabel setAdjustsFontSizeToFitWidth:YES];
 [self.view addSubview:myLabel];
 [myLabel release];
}

In my first view controller

MySecondViewcontroller *showMyViewcontroller = [[ MySecondViewcontroller    alloc]initWithNibName:@"MySecondViewcontroller" bundle:nil];;
showMyViewcontroller.myLabel = @"I cant set the text";
[self.navigationcontroller pushViewController: showMyViewcontroller animated:YES];
[showMyViewcontroller release];

What I am doing wrong here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灼痛 2025-01-13 15:09:51

尝试使用中间字符串来执行此操作,如下所示:

在 FirstViewController 中:

- (void)loadNextView {
    // load your view
    SecondViewController *vc = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    // set a string in your second view (NOT a label)
    vc.myString = @"Some String";

    // push it or whatever
    [self.navigaitonController pushViewController:vc animated:YES];
    [vc release];
}

在 SecondViewController.h 中

@interface SecondViewController : UIViewController {
    UILabel *myLabel;
    NSString *myString;
}

@property (nonatomic, retain) UILabel *myLabel;
@property (nonatomic, retain) NSString *myString;

在 SecondViewController.m 中

- (void)viewDidLoad {
    // view is now loaded, so we can set the label:
    myLabel.text = myString;
}

Try using an intermediate string to do it, like so:

In FirstViewController:

- (void)loadNextView {
    // load your view
    SecondViewController *vc = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    // set a string in your second view (NOT a label)
    vc.myString = @"Some String";

    // push it or whatever
    [self.navigaitonController pushViewController:vc animated:YES];
    [vc release];
}

In your SecondViewController.h

@interface SecondViewController : UIViewController {
    UILabel *myLabel;
    NSString *myString;
}

@property (nonatomic, retain) UILabel *myLabel;
@property (nonatomic, retain) NSString *myString;

In your SecondViewController.m

- (void)viewDidLoad {
    // view is now loaded, so we can set the label:
    myLabel.text = myString;
}
零度° 2025-01-13 15:09:51

您尝试将字符串分配给 UILabel,您需要将该字符串分配给 myLabel.text。此外,您还必须确保 UILabel 是可访问的,因此请确保它是一个属性。

You try to assign a string to a UILabel, you'll need to assign the string to myLabel.text. Also you'll have to make sure the UILabel is accessible, so make sure it's a property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文