将textfield值传递给字符串iphone

时间:2011-08-12 22:30:12

标签: iphone

在我的应用程序中,我有以下字段:

testMsg.fromEmail = @"support@gadsfd.co.uk";
testMsg.toEmail = @"%@",email;
testMsg.relayHost = @"smtp.gdsadfds.co.uk";
testMsg.requiresAuth = YES;
testMsg.login = @"support@dgadfda.co.uk";
testMsg.pass = @"ngdfadfdadfa";
testMsg.subject = @"The Money Shop Order Confirmation";
testMsg.wantsSecure = YES; // smtp.gmail.com doesn't work without TLS!

在testMsg.toEmail = @“some email”中;我想从textField.text = email传递字符串;

我该怎么做

2 个答案:

答案 0 :(得分:1)

必须为objective-C中的NSString对象分配对字符串的引用。这就是为什么源中的硬编码字符串必须以@符号开头。如果您希望引用现有字符串,例如textField.text。您可以使用简单的赋值表达式执行此操作。

textField.text = @"test@test.com";
testMsg.toEmail = textField.text;

答案 1 :(得分:1)

- (BOOL)validateEmailWithString:(NSString*)email{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; 
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; 
    return [emailTest evaluateWithObject:email];

}

if([self validateEmailWithString:textField.text){
    textMsg.toEmail = textField.text;
}else{
      NSLog(@"Email was not valid");
}

根据您的评论

- (NSMutableArray*)validateEmailWithString:(NSString*)emails {

    NSString *emailRegx = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";

    NSMutableArray *validEmails = [[NSMutableArray alloc] init];
    NSArray *emailList = [emails componentsSeparatedByString:@","];

    for (NSString *_email in emailList){

         NSPredicate *emailChecker = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; 
         if ([emailChecker evaluateWithObject:_email])
              [validEmails addObject:_email];
    }
    return [validEmails autorelease];
}