Objective C:int 在到达 If/Then 语句时突然变为第一个 if 条件
好吧,这是一个奇怪的问题...而不是发布大量代码,我将发布一些片段来解释:
添加了一个 UIDatePicker
这是一个操作表,我在 .h 文件中
NSInteger tagOfDateToUse;
在我的 .m 文件中我确保它的在 ViewDidLoad 上清除
tagOfDateToUse = 0;
我需要此变量,因为屏幕上有多个文本框可以拉出此操作表。所以我设置了这个 int,如果它是 100,它将转到一个文本框,如果它是 101,它会转到另一个文本框,
我在 IBAction 中设置了 tagOfDateToUse。然后在
(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
...做一些事情
它与第一个(100)一起工作正常,但是当我点击第二个按钮时,到目前为止它是101。
一旦它到达 if tagOfDateToUse is __ 它就认为它是 100,而到目前为止它已经是 101...if 语句中的什么导致它返回到 100...我在这里缺少什么...
if (tagOfDateToUse = 100) {vacation_starton.text = [[NSString alloc] initWithFormat:@"%@", [formatter stringFromDate:selectedDate]];
}
else if (tagOfDateToUse = 101)
{
vacation_endon.text = [[NSString alloc] initWithFormat:@"%@", [formatter stringFromDate:selectedDate]];
}
谢谢!
Ok this is a bizarre issue... rather than posting lots of code I will post a few snippets that will explain:
This is a action sheet that I have added a UIDatePicker
in my .h file
NSInteger tagOfDateToUse;
in my .m file I make sure its clear on ViewDidLoad
tagOfDateToUse = 0;
I need this variable because there are multiple text boxes on a screen that could be pulling up this action sheet. So I set this int so that if its a 100 it will go to one text box, if its a 101, it goes to another
I set the tagOfDateToUse in the IBAction. Then in the
(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
... Do some things
It works fine with the 1st one (100) but when I Hit my second button it is 101 up to this point.
As soon as it hits the if tagOfDateToUse is __ it thinks it's 100 when up to this point it has been 101... what in the if statement is causing it to go back to 100... What am I missing here...
if (tagOfDateToUse = 100) {vacation_starton.text = [[NSString alloc] initWithFormat:@"%@", [formatter stringFromDate:selectedDate]];
}
else if (tagOfDateToUse = 101)
{
vacation_endon.text = [[NSString alloc] initWithFormat:@"%@", [formatter stringFromDate:selectedDate]];
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您将值 100 分配给 tagOfDateToUse ,这是完全合法的,然后该值被评估为布尔条件,其中除 0 之外的任何值都是 true,只有 0 是 false。
尝试将其更改为这样...
You are assigning a value of 100 to tagOfDateToUse which is perfectly legal and then the value is evaluated as a boolean condition in which anything but 0 is true and only 0 is false.
Try changing it to this...
您缺少的可能是该条件中的作业。您的意思是编写
if (tagOfDateToUse == 100)
和else if (tagOfDateToUse == 101)
吗?What you're missing might be the assignment you have in that conditional. Did you mean to write
if (tagOfDateToUse == 100)
andelse if (tagOfDateToUse == 101)
?您在 if 语句中将 100 分配给 tagOfDateToUse 。你需要做一个比较。这是一个很容易犯的错字。
应该
确保您已打开“缺少大括号和圆括号”警告选项。它确实有助于追踪这些类型的问题。
You're assigning 100 to tagOfDateToUse in the if statement. You need to do a comparison instead. It's an easy typo to make.
should be
Make sure you have the "Missing Braces and Parentheses" warning option turned on. It really helps track these types of issues down.
一个非常常见的错误。使用赋值而不是相等,这是正确的。将其更改为
==
。有些人喜欢将常量放在左侧以避免这种情况。现在,如果您错过了一个
=
那么编译器会介意。A very common mistake. Assignment is used instead of equality which is true. Change it to
==
. Some people prefer placing constant on left to avoid this.Now if you miss one
=
then compiler will mind.