将 NSMutableString 分配给 UILabel 文本

发布于 2024-12-07 05:53:20 字数 1065 浏览 0 评论 0原文

我正在根据《Objective-C 2.0 编程》一书制作一个分数计算器应用程序。我正在使用 XCode 4.1 for Lion。我已经完全成功地在代码中输入了任何错误。但是,当我在应用程序运行时按数字按钮时,标签不会输入任何文本。当我按下数字按钮时,它应该找到 UIButton 发送者标记(我已分配的)

   -(IBAction)clickDigit:(UIButton *)sender {

int digit = sender.tag;

[self processDigit:digit];
    }

,并将其发送到以下代码:

  -(void)processDigit:(int)digit {

currentNumber = currentNumber * 10 + digit;

[displayString appendString:[NSString stringWithFormat:@"%i", digit]];
display.text = displayString;

NSLog(@"Processed");

    }

控制台打印已处理。说明这个按钮是起作用的,所以问题出在这行代码:

[displayString appendString:[NSString stringWithFormat:@"%i", digit]];
display.text = displayString;

display是一个UILabel,它是一个实例变量但它不是一个IBOutlet而是在它的属性中声明为一个IBOutlet。

@property (nonatomic, retain)IBOutlet UILabel *display;

我已使用代码助手连接将标签连接到 .xib 屏幕上。我可以从字面上将其与声明联系起来。所以我将它连接到该属性。

现在,问题在于display是一个UILabel,它的text属性需要一个NSString而不是NSMutableString,对吗?但是 NSString 无法附加字符串以及我需要它为该应用程序执行的许多其他方法。我怎样才能做到这一点?

I am making a fraction calculator app from the Programming In Objective-C 2.0 book. I am using XCode 4.1 for Lion. I have completely and successfully without any error typed in the code. But when I press a number button while the app is running, the label doesn't input any text. When I press a number button, it should find the UIButton senders tag (which I have allocated)

   -(IBAction)clickDigit:(UIButton *)sender {

int digit = sender.tag;

[self processDigit:digit];
    }

and it gets sent to this code:

  -(void)processDigit:(int)digit {

currentNumber = currentNumber * 10 + digit;

[displayString appendString:[NSString stringWithFormat:@"%i", digit]];
display.text = displayString;

NSLog(@"Processed");

    }

The console DOES print Processed. Indicating that this button is working, so the problem lies in this line of code:

[displayString appendString:[NSString stringWithFormat:@"%i", digit]];
display.text = displayString;

display is a UILabel, it is an instance variable but it is not an IBOutlet but is declared as an IBOutlet in its property.

@property (nonatomic, retain)IBOutlet UILabel *display;

I have connected the label on the .xib screen using the connection to code assistant thing. I can literally connect it to the declarations. So I connected it to the property.

Now, the problem lies in the fact that display is a UILabel and its text property requires an NSString not a NSMutableString right? But an NSString cannot append a string and many other methods that I need it to do for this app. How can I make this work?

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

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

发布评论

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

评论(3

筱武穆 2024-12-14 05:53:20

NSMutableStringNSString 大多数情况下可以互换。即,如果一个方法需要 NSString,您可以向它传递一个 NSMutableString。但是,反之则不行,而且它们经常被复制并变得不可变。即,即使您将 NSMutableString 传递给 UILabeltext 属性,它也始终会返回 NSString

我怀疑你这里还有其他问题。
也就是说,使用不可变字符串通常比使用可变字符串更容易。

您可以将此代码更改

[displayString appendString:[NSString stringWithFormat:@"%i", digit]];
display.text = displayString;

为以下代码:

display.text = [displayString stringByAppendingString:[NSString stringWithFormat@"%i",digit]];

因为 stringByAppendingString 使用接收器的内容 (displayString) 和作为参数传递的字符串 (<代码>[NSString stringWithFormat:@"%i",digit])。

我怀疑您的问题是 displayStringnil
添加一些 NSLog 以找出...

  1. -(IBAction)clickDigit:(UIButton *)sender 添加 NSLog(@"Sender is %@",sender)
  2. 到 <代码>-(void)processDigit:(int)digit add NSLog(@"数字是%i,显示字符串是%@,显示是%@",数字,显示字符串, display);

这应该让您了解哪些对象为零,或者在 Interface Builder 中连接不良。

NSMutableString and NSString are mostly interchangeable. I.e. if a method requires an NSString, you can pass it an NSMutableString. This doesn't work the other way, though, and they are often copied and made immutable. i.e. even if you pass an NSMutableString to UILabel's text property, it will always return an NSString back.

I suspect you've got some other issue going on here.
That said, it's often easier to work with immutable strings than mutable ones.

You can change this code:

[displayString appendString:[NSString stringWithFormat:@"%i", digit]];
display.text = displayString;

into this code:

display.text = [displayString stringByAppendingString:[NSString stringWithFormat@"%i",digit]];

Because stringByAppendingString makes a new immutable string object with the contents of the receiver (displayString) and the string passed as a parameter ([NSString stringWithFormat:@"%i",digit]).

I suspect your issue is displayString is nil.
Add some NSLog's to find out...

  1. to -(IBAction)clickDigit:(UIButton *)sender add NSLog(@"Sender is %@",sender)
  2. to -(void)processDigit:(int)digit add NSLog(@"Digit is %i and displayString is %@ and display is %@", digit, displayString, display);

This should give you an idea of what objects are nil, or badly connected in Interface Builder.

执笏见 2024-12-14 05:53:20

我面临同样的问题,我发现我在文件“Fraction_CalculatorViewController.m”的函数“viewDidLoad”中错过了这行代码,

 displayString =[NSMutableString stringWithCapacity:40];

这将解决问题iSA。

i face the same problem , i discovered that I missed this line of code at the function " viewDidLoad " at the file "Fraction_CalculatorViewController.m"

 displayString =[NSMutableString stringWithCapacity:40];

this will solve the problem iSA.

凹づ凸ル 2024-12-14 05:53:20

NSMutableString 一个NSString。更具体地说,它是一个子类。只需分配它即可工作。

An NSMutableString is an NSString. More specifically, it's a subclass. Just assign it and it will work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文