-6

I wonder how can I pass a UIlabel to a function ?

I wrote a function that gives a UILabel text from a table, but now i want to run the function with the UIlabel passed to the head of the function. Just like you would do with the text or int. Do I need a pointer or can I pass the label the normal way?

-(void)giveText:(NSString) *textinsert  
{
  label1.text=textinsert;
}

How could I save all the labels inside an array and run through the array?

4

3 回答 3

1

老实说,在阅读了您发表的评论后,我不相信您完全理解正在发生的事情。

-(void)giveText:(NSString) *textinsert将不起作用语法错误这种方法应该是-(void)giveText:(NSString *)textinsert,但这不是你的问题。

根据我对您所要求的内容的了解,您想将 a 传递UILabel给该方法。

所以你想要做的是

- (void)someMethod
{
    UILabel *label1 = [[UILabel alloc] init];
    [self giveText:label1];
}

- (void)giveText:(UILabel *)label
{
    // Do what ever with label.
}

那么发生了什么?

我们已经创建了giveText:以 aUILabel作为参数的方法,然后我们通过传入我们创建的参数来调用someMethod[self giveText:label1];方法UILabel

于 2013-06-13T13:38:59.753 回答
-1

尝试这个

 NSMutableArray *arr=[[NSMutableArray alloc]init];

//label Creation
UILabel *lbl=[[UILabel alloc]init];

//add into array
[arr addObject:lbl];


// get label From array one by one
for (UILabel *lbl in arr) {
 //pass label to function
    [self passLabel:lbl];
}

-(void)passLabel:(UILabel *)lbl

{
// do here
}
于 2013-06-13T13:10:35.800 回答
-2

您可以通过与例如 nsstring 相同的方式传递标签。

如果您只是想更改 ot 的文本,我认为您不应该将标签传递给函数。只需创建返回字符串的函数

label.text = [self getLabelText];
- (NSString *) getLabelText {
    NSString *text = @"text";
    return text;
}
于 2013-06-13T13:21:14.350 回答