HelloWorld Objective-C 帮助

发布于 2024-10-31 01:27:00 字数 374 浏览 1 评论 0原文

我对 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 技术交流群。

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

发布评论

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

评论(4

洛阳烟雨空心柳 2024-11-07 01:27:00

它应该是:

label.text = [NSString stringWithFormat:@"Hello %@", name];

stringWithFormat 使用“%”序列将值插入到它正在构建的字符串中。根据要插入的值类型,您可以使用不同的“%”序列:“%@”用于 Objective-C 对象,“%d”用于整数,等等。您可以使用多个“%”序列插入多个值,例如:

label.text = [NSString stringWithFormat:@"Hello %@ %@ and your %d friends", firstName, lastName, 7];

It should read:

label.text = [NSString stringWithFormat:@"Hello %@", name];

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:

label.text = [NSString stringWithFormat:@"Hello %@ %@ and your %d friends", firstName, lastName, 7];
滥情哥ㄟ 2024-11-07 01:27:00

试试这个:

[NSString stringWithFormat:@"Hello %@", name];

Try this:

[NSString stringWithFormat:@"Hello %@", name];
-(IBAction)sayHello:(id)sender{
  NSString *name = [textField text];
  label.text = [NSString stringWithFormat:@"Hello %@", name];
}

您必须添加 %@,以便它将您的姓名字符串放入格式化字符串中。

-(IBAction)sayHello:(id)sender{
  NSString *name = [textField text];
  label.text = [NSString stringWithFormat:@"Hello %@", name];
}

You have to add %@, so that it will place your name string in the formatted string.

掀纱窥君容 2024-11-07 01:27:00

您需要一个格式说明符:

label.text = [NSString stringWithFormat:@"Hello %@", name];

%@ 格式说明符表示“NSObject 引用”。要格式化其他数据类型 - int、double 等,您应该使用 %d、%f 等。

You need a format specifier:

label.text = [NSString stringWithFormat:@"Hello %@", name];

The %@ format specifier means "an NSObject reference". For formatting other datatypes - int, double, etc. you should use %d, %f and so on.

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