2013-09-24 66 views
-4

如何在目標c中串聯字符串?串連在一起的目標c字符串

- (IBAction)emailButton:(id)sender { 
MFMailComposeViewController *mailContoller = [[MFMailComposeViewController alloc]init]; 
[mailContoller setMailComposeDelegate:self]; 
NSString *email = @"*******gmail.com"; 
NSString *email1 = @"*******@hotmail.co.uk"; 
NSArray *emailArray = [[NSArray alloc]initWithObjects:email, email1, nil]; 
NSString *message = [NSString stringWithFormat:@"%@\n%@\n%@", 
textField1.text, textField2.text, textField3.text]; 
[mailContoller setMessageBody:message isHTML:NO]; 
[mailContoller setToRecipients:emailArray]; 
[mailContoller setSubject:@"IT WORKS!"]; 
[self presentViewController:mailContoller animated:YES completion:nil]; 
} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
[[self myTextView] resignFirstResponder]; 
} 

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error 
{ 
[self dismissViewControllerAnimated:YES completion:nil]; 
} 
@end 

對此的任何幫助將不勝感激。

謝謝

+0

@DavidRönnqvist是的。但刪除它。 –

回答

2

最簡單的例子:

NSString* concatenatedString = [stringA stringByAppendingString: stringB]; 

從你的代碼,這樣的:

NSString *message = [NSString stringWithFormat:@"%@\n%@\n%@", textField1.text, textField2.text, textField3.text]; 

可能是更好的表述爲:

NSString *message = [@[textField1.text, textField2.text, textField3.text] componentsJoinedByString: @"\n"]; 

+stringWithFormat:比較昂貴,除了簡單的級聯發生之外。

+0

我只是不斷收到一條消息,說我未輸入此代碼時未聲明的變量。 – ash

+0

當然,你需要用你的字符串變量替換'stringA'和'stringB'。 – ipmcc