在“if语句错误”上中止IBAction

时间:2013-10-18 01:23:57

标签: ios objective-c if-statement ibaction abort

我正在尝试进行首次用户注册。注册将由4个文本字段进行,这些文本字段需要由用户编辑并在按下按钮后保存(IBAction)。当1个字段为空时,需要中止此IBAction。将向用户显示警告文本,他/她将需要更正问题,然后再继续将文本数据保存到解析服务器/

我已经将我的4个Uitextfields连接到.m脚本了,我想我写了一个很好的if语句。但是如何在警告文本后停止我的脚本?

代码.M:

- (IBAction)REGISTERNEWUSER:(id)sender
{

    //DEFINE STRINGS FOR MODULE
    NSString *NICKNAME = Nickname.text;
    NSString *USERNAME = Username.text;
    NSString *PASSWORD = Password.text;
    NSString *EMAIL = Email.text;
    NSString *ERRORREGISTRATION = @"Please fill up all fields above before proceeding with the  registration...";

    //CHECK IF ALL REGISTRATION FIELDS ARE FILLED
    if ([Nickname.text length] > 0 || [Username.text length] > 0 || [Password.text length] > 0 || [Email.text length] > 0 )
    {
    }
    ErrorRegistration.text = ERRORREGISTRATION;

    //
    // METHODE NEEDS TO STOP HERE
    // BUT AT MOMENT CONTINUES TO SEND DATA TO PARSE
    //
    {

    }


    //SEND DATA TO PARSE SERVER
    PFObject *User = [PFObject objectWithClassName:@"USER"];
    [User setValue:NICKNAME forKey:@"Nickname"];
    [User setValue:USERNAME forKey:@"Username"];
    [User setValue:PASSWORD forKey:@"Password"];
    [User setValue:EMAIL forKey:@"Email"];
    [User save];
}

提前感谢您提供的任何帮助。 亲切的问候, 留置权。

1 个答案:

答案 0 :(得分:0)

你想要这样的东西:

if ([Nickname.text length] == 0 || [Username.text length] == 0 || [Password.text length] == 0 || [Email.text length] == 0 ) {
    // Not all fields are entered
    ErrorRegistration.text = ERRORREGISTRATION;
} else {
    // All fields are entered
    //SEND DATA TO PARSE SERVER
    PFObject *User = [PFObject objectWithClassName:@"USER"];
    [User setValue:NICKNAME forKey:@"Nickname"];
    [User setValue:USERNAME forKey:@"Username"];
    [User setValue:PASSWORD forKey:@"Password"];
    [User setValue:EMAIL forKey:@"Email"];
    [User save];
}

BTW - 修复大写锁定键。方法和变量名应该使用camelCase(以小写字母开头)。类名应该使用CamelCase(以大写字母开头)。

if ([nickname.text length] == 0 || [username.text length] == 0 || [password.text length] == 0 || [email.text length] == 0 ) {
    // Not all fields are entered
    errorRegistration.text = @"Please fill up all fields above before proceeding with the  registration...";
} else {
    // All fields are entered
    //SEND DATA TO PARSE SERVER
    PFObject *user = [PFObject objectWithClassName:@"USER"];
    [user setValue:nickname.text forKey:@"Nickname"];
    [user setValue:username.text forKey:@"Username"];
    [user setValue:password.text forKey:@"Password"];
    [user setValue:email.text forKey:@"Email"];
    [user save];
}

在这种情况下不需要所有额外的变量。