HelloWorld Objective-C 帮助
我对 Objective-C iPhone 编程非常陌生。我会在这个问题上采用试错法:P。所以我的问题与获得名字有关。这是我的代码:
-(IBAction)sayHello:(id)sender{
NSString *name = [textField text];
label.text = [NSString stringWithFormat:@"Hello ", name];
它的作用是从 UITextField 获取文本(我已经有了这个设置)并输出到 UILabel。我认为这会起作用,但显然不行,因为它只输出“Hello”。名字不见了。我们将不胜感激:)
谢谢,
埃里克
编辑:感谢所有回复。成功了! :)
I'm Very new to Objective-C iPhone programming. I'm kinda gonna go with the trial-and-error method on this one:P. So my question has to do with getting a name. Here's my code:
-(IBAction)sayHello:(id)sender{
NSString *name = [textField text];
label.text = [NSString stringWithFormat:@"Hello ", name];
What this does is get the text from a UITextField (I already have this setup) and outputs to a UILabel. I would think this would work, but apparently not, because it just outputs "Hello ". Missing the name. Help would be appreciated :)
Thanks,
Eric
EDIT: Thanks to all that responded. It worked! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它应该是:
stringWithFormat
使用“%”序列将值插入到它正在构建的字符串中。根据要插入的值类型,您可以使用不同的“%”序列:“%@”用于 Objective-C 对象,“%d”用于整数,等等。您可以使用多个“%”序列插入多个值,例如:It should read:
stringWithFormat
uses "%" sequences to insert values into the string it's building. You use different "%" sequences depending on what type of value you're inserting: "%@" is for Objective-C objects, "%d" is for integers, and so forth. You can insert multiple values by using multiple "%" sequences, for example:试试这个:
Try this:
您必须添加
%@
,以便它将您的姓名字符串放入格式化字符串中。You have to add
%@
, so that it will place your name string in the formatted string.您需要一个格式说明符:
%@ 格式说明符表示“NSObject 引用”。要格式化其他数据类型 - int、double 等,您应该使用 %d、%f 等。
You need a format specifier:
The %@ format specifier means "an NSObject reference". For formatting other datatypes - int, double, etc. you should use %d, %f and so on.