English 中文(简体)
目标C-使用变量作为选择器
原标题:Objective C - Use Variable as selector

I just wanted to ask how it is possible to use a variable as selector. My code looks like this:

NSString *stunde = [lesson objectForKey:@"stunde"]; // value is t1s1, then t1s2 then t1s3 etc.
t1s1.text=subject;

这毫无问题。但由于我必须循环浏览40个标签,这样做会很棒:

NSString *stunde = [lesson objectForKey:@"stunde"];
        **stunde**.text=subject;

如何使用从for循环中的json字符串中获得的值来动态地与标签对话。

结果应该如下所示:

NSString *stunde = [lesson objectForKey:@"stunde"];
        t1s1.text=@"English";

NSString *stunde = [lesson objectForKey:@"stunde"];
        t1s2.text=@"German"; 

NSString *stunde = [lesson objectForKey:@"stunde"];
        t1s3.text=@"Business Studies";

我希望你能帮助我,知道我的意思

提前感谢

更新:

我刚开始这样工作(硬编码):

labels[0] = t1s1;
    labels[1] = t1s2;
    labels[2] = t1s3;
    for (NSDictionary *lessons in list)
    {
        int TotalLessons = [list count];

        NSString *fach = [lessons objectForKey:@"fach"];
        UILabel *stunde = [lessons objectForKey:@"stunde"];
        //labels[counter]=stunde;

        labels[counter].text=fach;


        counter=counter+1;
        if(counter>=TotalLessons){
            break;
        }



    }

but as there will be 40-50 labels I would like to add the label_names dynamically to the array: like this:

UILabel *stunde = [lessons objectForKey:@"stunde"];
labels[counter]=stunde;

值“stunde”将被定义为UILabel,然后添加到数组中。但为什么应用程序崩溃了?怎么了?:(

谢谢

问题回答

您应该首先初始化一个字典,如下所示:

NSDictionary *Labels = [[NSDictionary alloc] initWithObjectsAndKeys:
    LabelObject1, @"LabelObjectName1",
    LabelObject2, @"LabelObjectName2",
    LabelObject3, @"LabelObjectName3",
    ...
    LabelObjectN, @"LabelObjectNameN",
    nil];

则可以通过以下方式访问每个标签:

NSString *stunde = [lesson objectForKey:@"stunde"];
((UILabel *)[Labels objectForKey:stunde]).text = @"Some text";




相关问题
passing form variables into a url with php

I have the following form which allows a user to select dates/rooms for a hotel reservation. <form action="booking-form.php" method="post"> <fieldset> <div class="select-date">...

Error: "Cannot modify the return value" c#

I m using auto-implemented properties. I guess the fastest way to fix following is to declare my own backing variable? public Point Origin { get; set; } Origin.X = 10; // fails with CS1612 Error ...

C-style Variable initialization in PHP

Is there such a thing as local, private, static and public variables in PHP? If so, can you give samples of each and how their scope is demonstrated inside and outside the class and inside functions?

C#/.NET app doesn t recognize Environment Var Change (PATH)

In my C# app, I am programmatically installing an Oracle client if one is not present, which requires adding a dir to the PATH system environment variable. This all works fine, but it doesn t take ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

How to dynamically generate variables in Action Script 2.0

I have a for loop in action script which I m trying to use to dynamically create variable. Example for( i = 0 ; i &lt 3 ; i++) { var MyVar+i = i; } after this for loop runs, i would like to ...

热门标签