将 NSMutableString 分配给 UILabel 文本
我正在根据《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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NSMutableString
和NSString
大多数情况下可以互换。即,如果一个方法需要NSString
,您可以向它传递一个NSMutableString
。但是,反之则不行,而且它们经常被复制并变得不可变。即,即使您将NSMutableString
传递给UILabel
的text
属性,它也始终会返回NSString
。我怀疑你这里还有其他问题。
也就是说,使用不可变字符串通常比使用可变字符串更容易。
您可以将此代码更改
为以下代码:
因为
stringByAppendingString
使用接收器的内容 (displayString
) 和作为参数传递的字符串 (<代码>[NSString stringWithFormat:@"%i",digit])。我怀疑您的问题是
displayString
是nil
。添加一些 NSLog 以找出...
-(IBAction)clickDigit:(UIButton *)sender
添加NSLog(@"Sender is %@",sender)
NSLog(@"数字是%i,显示字符串是%@,显示是%@",数字,显示字符串, display);
这应该让您了解哪些对象为零,或者在 Interface Builder 中连接不良。
NSMutableString
andNSString
are mostly interchangeable. I.e. if a method requires anNSString
, you can pass it anNSMutableString
. This doesn't work the other way, though, and they are often copied and made immutable. i.e. even if you pass anNSMutableString
toUILabel
'stext
property, it will always return anNSString
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:
into this code:
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
isnil
.Add some NSLog's to find out...
-(IBAction)clickDigit:(UIButton *)sender
addNSLog(@"Sender is %@",sender)
-(void)processDigit:(int)digit
addNSLog(@"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.
我面临同样的问题,我发现我在文件“Fraction_CalculatorViewController.m”的函数“viewDidLoad”中错过了这行代码,
这将解决问题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"
this will solve the problem iSA.
NSMutableString
是一个NSString
。更具体地说,它是一个子类。只需分配它即可工作。An
NSMutableString
is anNSString
. More specifically, it's a subclass. Just assign it and it will work.