如何从另一个对象设置 UItextlabel 文本

发布于 2024-12-04 05:38:52 字数 779 浏览 1 评论 0原文

我在 NSobject 中设置一个 NSString,我想在不同的视图中将值传递给 uitableviewcell UIlable ?我很想知道正确设置这个的代码是什么,因为我觉得我在做正确的事情,但它只是没有改变标签中的文本。

//inside tableview/forRowAtIndexPath
    //load VehicleSearchObjects
    VehicleSearchObject *vehicleSearchObject = [[VehicleSearchObject alloc] init];

    if(indexPath.section == 0)
    {
        if(indexPath.row == 0)
        {
            UILabel *label1;
            label1 = (UILabel *)[cell viewWithTag:1];
            label1.text = @"Manufacture";

            UILabel *label2;
            label2 = (UILabel *)[cell viewWithTag:2];
            label2.text = [vehicleSearchObject manufacturerString]; //this is where I try to set the text... but nothing is happening, am I doing it right or should I be doing something else?

I am setting an NSString inside an NSobject in which I want to pass the value over to uitableviewcell UIlable in a different view? I am woundering what the code would be to set this correctly as I feel like I am doing the right thing but its just not changing the text in the label..

//inside tableview/forRowAtIndexPath
    //load VehicleSearchObjects
    VehicleSearchObject *vehicleSearchObject = [[VehicleSearchObject alloc] init];

    if(indexPath.section == 0)
    {
        if(indexPath.row == 0)
        {
            UILabel *label1;
            label1 = (UILabel *)[cell viewWithTag:1];
            label1.text = @"Manufacture";

            UILabel *label2;
            label2 = (UILabel *)[cell viewWithTag:2];
            label2.text = [vehicleSearchObject manufacturerString]; //this is where I try to set the text... but nothing is happening, am I doing it right or should I be doing something else?

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

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

发布评论

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

评论(3

美人骨 2024-12-11 05:38:52

如果更改 label1 的文本有效,那么我会检查您尝试访问的标签是否确实属于标签 2,如果为 true,则manufacturerString 确实返回了任何东西。

If changing label1's text works, then I'd look into whether or not the label you're trying to access is indeed of tag 2, and if true, if manufacturerString is indeed returning anything.

空心空情空意 2024-12-11 05:38:52

每次调用该方法时都会初始化vehicleSearchObject。我希望 manufacturerString 不是 nil。另外,如果您不分配任何内存,UILabel 可能没有内存。尝试将其设为@property。您可以在调试器控制台中检查它是否有内存。

编辑:声明 UILabel * label1 并在界面或 .h 文件和 中写入 @property(nonatomic,retain) UILabel * label1; >@synthesize label1; 在您的实现或 .m 文件中。在 UIView 初始化方法(如 viewDidLoad: 等)中分配/初始化标签。这会将内存分配给 label1,其范围涵盖所有函数。现在,不要再写UILabel *label1;。只需根据您的要求继续设置文本即可。对于label2 也是如此。

You initialize vehicleSearchObject every time the method is called. I hope manufacturerString is not nil. Also, UILabel might not have memory if you do not allocate it any. Try making it a @property. You can check if it has memory in the debugger console.

EDIT: Declare UILabel * label1 and write @property(nonatomic,retain) UILabel * label1; in your interface or .h file and @synthesize label1; in your implementation or .m file. Alloc/init the labels in your UIView initializing methods like viewDidLoad: etc. This allocates memory to label1 with scope across all functions. Now, do not write UILabel *label1; again. Just keep setting the text according to your requirements. Similarly for label2.

想念有你 2024-12-11 05:38:52

您可能想尝试 [UILabel setText:(NSString)] 而不是 UILabel.text = (String)。

You may want to try [UILabel setText:(NSString)] instead of UILabel.text = (String).

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