简体   繁体   English

如何为UITextField分配默认值?

[英]How do I assign a default value to a UITextField?

My iOS app crashes every time a user hits 'next' when one of the fields (they're strings) is empty. 字段之一(它们是字符串)为空时,每次用户单击“ next”,我的iOS应用程序都会崩溃。 How do I make it to where this doesn't happen? 我如何才能做到这一点呢?

emailBody = [emailBody stringByAppendingString:nameField.text];

Here's one of the lines that is causing the crash; 这是导致崩溃的行之一; it's trying to pull the data from nameField.text, but in some cases it's empty and this crashes it. 它试图从nameField.text中提取数据,但是在某些情况下,它为空,这会使它崩溃。

You shouldn't assign a default value - instead you should secure your code so it does not crash. 您不应分配默认值-而是应保护代码,以免崩溃。

In this case, you could do the following: 在这种情况下,您可以执行以下操作:

if (nameField.text) {
    emailBody = [emailBody stringByAppendingString:nameField.text];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM