Ios 将UILabel转换为NSString时,NSString会自动生成值';0';

Ios 将UILabel转换为NSString时,NSString会自动生成值';0';,ios,objective-c,cocoa-touch,nsstring,uilabel,Ios,Objective C,Cocoa Touch,Nsstring,Uilabel,我正在开发一个计算器应用程序,“等于”按钮就是不想工作 当用户输入一个数字并按下“乘法”按钮时,UILabel上的当前值将转换为整数,然后存储在NSInteger中。当用户开始输入下一组数字时,标签将切换到他们输入的任何数字 当用户按下“equals”时,应该发生的是当前的UILabel被转换为NSString,然后转换为NSInteger,并乘以先前存储的结果。然后,该新整数被转换为NSString,并设置为UILabel 问题是,当代码将标签设置为字符串时,它会自动将所有内容更改为0。现在,

我正在开发一个计算器应用程序,“等于”按钮就是不想工作

当用户输入一个数字并按下“乘法”按钮时,
UILabel
上的当前值将转换为整数,然后存储在
NSInteger
中。当用户开始输入下一组数字时,标签将切换到他们输入的任何数字

当用户按下“equals”时,应该发生的是当前的
UILabel
被转换为
NSString
,然后转换为
NSInteger
,并乘以先前存储的结果。然后,该新整数被转换为
NSString
,并设置为
UILabel

问题是,当代码将标签设置为字符串时,它会自动将所有内容更改为0。现在,我不知道
UILabel
是否只是不指向
NSString
,或者当将
NSString
设置为
UILabel
的内容时,是否没有从
UILabel
拾取内容

代码如下:

- (IBAction)equalsWhenPressed:(id)sender {

    if ([operation  isEqual:@"x"]) {

        //get the UILabels text
        NSString *flabb = self.screen.text;

        //converting the string to a integer
        NSInteger yahh = [flabb integerValue];

        //doing the multiplication
        NSInteger actualProblem = yahh * storedResult;

        NSString *result = [NSString stringWithFormat:@"%ld", (long)actualProblem];

        self.screen.text = result;

       operation = nil;
   }
}

好的,让我们看看您的代码:

//get the UILabels text
NSString *flabb = self.screen.text; // <-- This effectually does nothing  

//converting the string to a integer
NSInteger yahh = [flabb integerValue];
我不是要挑剔你的代码,但我认为这一点是相关的。现在,如果您在标签中输入的文本不是由NSLog输出的:

//get the UILabels text
NSString *flabb = self.screen.text; // <-- This effectually does nothing 

NSLog(@"%@", flabb); // <-- This is directly printing what is in the UILabel "screen".    
NSLog(@"%@", self.screen.text); // This will be the same.

//converting the string to a integer
NSInteger yahh = [flabb integerValue];
它可以打印一个。请检查代码以确保正确访问UILabel文本

这里有一个测试供您尝试(使用您的原始方法):

if(self.screen!=nil)
{
//获取UILabels文本

NSString*flabb=self.screen.text;//能否请您将下面的行添加到代码中,并告诉我们您在输出窗口中得到了什么? 除非字符串不是数字,否则代码看起来是正确的,并且intValue在转换时返回0(它不会触发异常)


“将所有内容更改为0”是什么意思?请清楚地告诉我们您发布的代码有什么问题。
flabb
的值是多少?
storedResult
的值是多少?因此您是说字符串中的转换值(yahh)总是等于零吗?storedResult是用户在点击次数之前输入的内容。这样保存和输入很好。问题是flabb输出0。当我将屏幕设置为flabb时,我尝试了一些东西,但忘记删除它。我修复了代码。谢谢。@DylanCrocker不,flabb的值在不支持时总是等于零很抱歉。你仍然没有说出“将所有内容更改为0”的意思。什么是“所有内容”?我很确定我正确访问了UIlabel文本。我只在这种特殊情况下遇到这个问题。没有其他地方。那么你必须发布更多的代码,因为问题不在发布的内容中。你说字符串flabb为nil(0指针)或者@“0”?我只是把完整的.m文件放在了问题中。同样,根据我从NSLogging中了解到的情况,flabb正在输出@“0”当它不应该的时候,我给你的答案增加了更多的信息。希望它能帮助你更清楚地理解正在发生的事情。请考虑投票给那些试图帮助你的人的评论/答案。谢谢。我真的很感谢你,不幸的是,因为我是一个新的堆栈溢出者,我无法评价任何事情。我会喜欢的。为了解决这个问题,我回答了,但没有任何效果。我明天会再试一次,也许会重新写一些东西。再次,非常感谢。
//get the UILabels text
NSString *flabb = self.screen.text; // <-- This effectually does nothing 

NSLog(@"%@", flabb); // <-- This is directly printing what is in the UILabel "screen".    
NSLog(@"%@", self.screen.text); // This will be the same.

//converting the string to a integer
NSInteger yahh = [flabb integerValue];
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 20, 20)];
label.text = @"1";
NSLog(@"%i", [label.text intValue]);
if (self.screen != nil)
{
  //get the UILabels text
  NSString *flabb = self.screen.text; // <-- This effectually does nothing 

  NSLog(@"%@", flabb); // <-- This is directly printing what is in the UILabel "screen".    
  NSLog(@"%@", self.screen.text); // This will be the same.

  //converting the string to a integer
  NSInteger yahh = [flabb integerValue];

  // Rest of your code here...
}
else
{
  NSLog(@"%@",@"screen is nil!");
}
//get the UILabels text
NSString *flabb = self.screen.text;

// add this line
NSLog(@"flabb == %@" , flabb );

//converting the string to a integer
NSInteger yahh = [flabb integerValue];