如何从另一个对象设置 UItextlabel 文本
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果更改
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 tag2
, and if true, ifmanufacturerString
is indeed returning anything.每次调用该方法时都会初始化
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 hopemanufacturerString
is notnil
. 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 yourUIView
initializing methods likeviewDidLoad:
etc. This allocates memory tolabel1
with scope across all functions. Now, do not writeUILabel *label1;
again. Just keep setting the text according to your requirements. Similarly forlabel2
.您可能想尝试 [UILabel setText:(NSString)] 而不是 UILabel.text = (String)。
You may want to try [UILabel setText:(NSString)] instead of UILabel.text = (String).