在 UILabel 中打印字母

发布于 2024-11-03 15:08:02 字数 877 浏览 1 评论 0原文

大家好, 在我的iPhone应用程序中想要在UILabel中逐个打印一系列字符串,但现在只显示最终的字符串。 我已经尝试过 NSTIMER,但它不起作用。 任何人都可以帮助我。提前致谢

这是我的代码:

//changing strings to char 
    char *cname1=[n1 cStringUsingEncoding:NSASCIIStringEncoding];
    char *cname2=[n2 cStringUsingEncoding:NSASCIIStringEncoding];


    for (int i = 0; i < [n1 length]; i++)
        {
            for (int j = 0; j < [n2 length]; j++){
            if ((cname1[i]==cname2[j]))
            {

                cname1[i]='*';
                cname2[j]='*';

                myString1 = [NSString stringWithFormat:@"%s", cname1];

                myString2 = [NSString stringWithFormat:@"%s", cname2];

                [self printlabel];
                break;
            }


    }

    -(void)printlabel{
        nam1.text = myString1; //set label1
        nam2.text = myString2; //set label2

        }

Hai all,
In my iPhone app want to print a series of strings in UILabel one by one,but only the final string is displaying now.
I have tried NSTIMER,but it doesn't work.
can anyone help me.thanks in advance

Here is my code:

//changing strings to char 
    char *cname1=[n1 cStringUsingEncoding:NSASCIIStringEncoding];
    char *cname2=[n2 cStringUsingEncoding:NSASCIIStringEncoding];


    for (int i = 0; i < [n1 length]; i++)
        {
            for (int j = 0; j < [n2 length]; j++){
            if ((cname1[i]==cname2[j]))
            {

                cname1[i]='*';
                cname2[j]='*';

                myString1 = [NSString stringWithFormat:@"%s", cname1];

                myString2 = [NSString stringWithFormat:@"%s", cname2];

                [self printlabel];
                break;
            }


    }

    -(void)printlabel{
        nam1.text = myString1; //set label1
        nam2.text = myString2; //set label2

        }

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

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

发布评论

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

评论(2

↘人皮目录ツ 2024-11-10 15:08:02

假设您在 NSMutableArray 中有 10 个 UILabel

NSMutableArray* myAllLabel ;
int labelIndex = 0;

使用下面的代码作为参考。

- (void) startDisplayingLabel {
     labelIndex = 0;
    [NSTimer scheduledTimerWithInterval:2.0f target:self selector:@selector(showElapsedTime:) userInfo:nil repeats:YES];
}

showElapsedTime 将在您提供的 2.0 秒延迟后调用。

-(void) showElapsedTime: (NSTimer *) timer {

    if(labelIndex == 10)
    {
      [timer invalidate];
    }
    else 
    {
         //Now you have your mylabel , you could add it to the superview 
         UILabel* mylabel = (UILabel*)[myAllLabel objectAtIndex:labelIndex];
         // Write your code here 

    }
    labelIndex++;
}

调用 startDisplayingLabel 一次,我将在 2.0 秒后显示每个 UILabel。

Let's suppose, you have 10 UILabel in NSMutableArray.

NSMutableArray* myAllLabel ;
int labelIndex = 0;

Use the below code as reference.

- (void) startDisplayingLabel {
     labelIndex = 0;
    [NSTimer scheduledTimerWithInterval:2.0f target:self selector:@selector(showElapsedTime:) userInfo:nil repeats:YES];
}

showElapsedTime will be called after 2.0 second of delay, you provide.

-(void) showElapsedTime: (NSTimer *) timer {

    if(labelIndex == 10)
    {
      [timer invalidate];
    }
    else 
    {
         //Now you have your mylabel , you could add it to the superview 
         UILabel* mylabel = (UILabel*)[myAllLabel objectAtIndex:labelIndex];
         // Write your code here 

    }
    labelIndex++;
}

Call startDisplayingLabel once, i will display each UILabel after exactly 2.0 second.

征棹 2024-11-10 15:08:02

可能 NSThread将帮助您

尝试将代码更改为,

 for (int i = 0; i < [n1 length]; i++)
        {
            for (int j = 0; j < [n2 length]; j++){
            if ((cname1[i]==cname2[j]))
            {

                cname1[i]='*';
                cname2[j]='*';

                myString1 = [NSString stringWithFormat:@"%s", cname1];

                myString2 = [NSString stringWithFormat:@"%s", cname2];

                [self printlabel];
                break;
            }
        [NSThread sleepForTimeInterval:2.0];//sleeps for 2 seconds.
    }

Probably NSThread will help you

Try changing your code as,

 for (int i = 0; i < [n1 length]; i++)
        {
            for (int j = 0; j < [n2 length]; j++){
            if ((cname1[i]==cname2[j]))
            {

                cname1[i]='*';
                cname2[j]='*';

                myString1 = [NSString stringWithFormat:@"%s", cname1];

                myString2 = [NSString stringWithFormat:@"%s", cname2];

                [self printlabel];
                break;
            }
        [NSThread sleepForTimeInterval:2.0];//sleeps for 2 seconds.
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文