删除第二个标签,然后第一个标签,无法正常工作的方法 [英] Method To Delete 2nd Label, Then First Label, Not Functioning Correctly

查看:76
本文介绍了删除第二个标签,然后第一个标签,无法正常工作的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我缺少明显的东西,但是我看不到它.此方法旨在将标签的文本与文本框的文本进行比较,然后删除该文本.因此,如果第一个标签显示为"Puppy",第二个标签显示为小猫",并且文本框显示为小猫",则该方法应删除第二个标签的文本,并保留第一个标签的文本.如果第二个标签为空白,则该方法应删除第一个标签的文本.

I know I am missing something obvious, but I just cannot see it. This method is meant to compare the text of a label to the text of a text box, and then delete the text. So if the 1st label reads "Puppy" and the 2nd label reads "Kittens," and the text box says "Kittens," the method should delete the text of the 2nd label and leave the 1st label's text. If the 2nd label is blank, then the method should delete the text of the 1st label.

但是,无论我如何使用该方法,它都会删除第二个标签而不删除第一个标签,或者删除两个标签,或者删除两个标签.这是我尝试过的 (lblFabric1是第一个标签,lblFabric2是第二个标签,txtType是文本框):

But no matter how I mess with the method, either it deletes the 2nd label but not the 1st, deletes both of them, or deletes neither of them. Here's what I've tried (lblFabric1 is the 1st label, lblFabric2 is the 2nd label, txtType is the text box):

-(IBAction)btnDelete:(id)sender
{
  if ((self.lblFabric2.text== self.txtType.text))
   {
     self.lblFabric2.text = @"";
   }
  else if ((self.lblFabric2.text != self.txtType.text))
   {
     self.lblFabric1.text=@"";
   }
}

它删除第二个标签,但不删除第一个标签.如果我尝试将其他"设置为:

It deletes the 2nd label, but not the 1st label. If I try to set the "Else if" to:

else if ((self.lblFabric2.text==@""))

这给了我一个错误("直接比较字符串文字具有未定义的行为.)我只是用错误的方式处理此问题吗?我缺少什么?

it gives me an error (""Direct comparison of a string literal has undefined behavior.") Am I just going about this the wrong way? What am I missing?

推荐答案

在目标C中,不应使用==或!=进行字符串比较.您需要使用isEqualToStringisEqual方法.

You should not use == or != for string comparison in Objective C. You need to use the isEqualToString or isEqual method.

if (([self.lblFabric2.text isEqualToString:self.txtType.text]))

使用==或!=时,您正在比较存储字符串的指针.

When you use == or != you are comparing the pointers where the strings are stored.

这篇关于删除第二个标签,然后第一个标签,无法正常工作的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆