2011-08-12 32 views
-3

在我的應用程序有以下字段:傳遞文本字段值的字符串iphone的

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

在testMsg.toEmail = @「一些電子郵件」;我想從textField.text = email傳遞字符串;

我如何能做到這一點在Objective-C

+0

-1非常不清楚的問題 –

+0

爲什麼不明確的?它清楚地表明我想通過testMsg.toEmail = @「」; in「來自textField.test的值。什麼是不清楚的。請讓我知道 – Mann

+0

由於您要求將作業分配給作業,因此尚不清楚。 - 非常令人困惑 –

回答

1
- (BOOL)validateEmailWithString:(NSString*)email{ 
    NSString *emailRegex = @"[A-Z0-9a-z._%+-][email protected][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._%+-][email protected][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]; 
} 
+0

我希望你輸入有效的電子郵件地址到你的文本字段,如果不是,那麼也有解決方案。 :) –

+0

我得到了我的答案。感謝隊友的幫助。我還在想更多的事情。如果我必須通過兩封電子郵件。那我怎麼能通過 – Mann

+1

根據你的問題編輯我的答案:) –

1

一個NSString對象必須分配給一個字符串的引用。這就是爲什麼源代碼中的硬編碼字符串必須以@符號開頭。如果你想引用一個已經存在的字符串,例如textField.text。你可以用一個簡單的賦值表達式來完成。

textField.text = @"[email protected]"; 
testMsg.toEmail = textField.text; 
+0

我收到了textField.text = MSG。我之前已將電子郵件ID存儲在MSG中。 – Mann

+0

對不起textMsg.toEmail = textField.text起作用。謝謝 – Mann

相關問題