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

标签 iphone ios uiviewcontroller uilabel

在我的 View Controller 中, 我在这里创建一个标签。

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

我在中创建标签

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

在我的第一个 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];

我这里做错了什么

最佳答案

尝试使用一个中间字符串来做到这一点,就像这样:

在 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;
}

关于iphone - 在另一个类中设置标签文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9332082/

相关文章:

iphone - 使用 NSKeyValueCoding 将复杂的 JSON API 响应消费到 NSObjects

ios - 如何从 iOS 中的 Facebook SDK 4.0 获取用户名

iOS动画对象回到原来的位置

iphone-sdk-3.0 - UITabBarController、MoreNavigationController 和设备轮换的 chalice

model-view-controller - 以编程方式切换 MVC View Controller - iOS

iphone - NSString 问题

iphone - 在没有 Airplay 设备的情况下显示 AirPlay 图标?

iphone - viewDidAppear 未调用但 viewWillAppear 调用仅出现在 iOS5 中

ios - 快速调整父 UIView 的自动布局约束,其标签数量为 ' N ',作为 subview

ios - 从 TableView 单元格到另一个 View Controller 的 Segue 转换仅在任意时间有效 - 为什么?