如何在iphone中创建联系我们表格

时间:2012-10-31 06:32:52

标签: iphone objective-c http-post

我创建了一个联系我们表单,表单中有三个文本字段(名称,电子邮件,电话号码)和1个textview(消息)和1个动作按钮。我已将所有验证放在表单上,​​但我的问题是,当有人填写表单并按下操作按钮时,表单中的所有信息都应发送到我的个人邮件ID。请为我编写代码以提交邮件

#import "ViewController.h"

@implementation ViewController
@synthesize name, phone_no,email,message,mylabel;

-(IBAction)submit:(id)sender

{
    NSString *ename = [[NSString alloc] initWithString:name.text];
    NSString *ephone = [[NSString alloc] initWithString:phone_no.text];
    NSString *eemail = [[NSString alloc] initWithString:email.text];
    NSString *emessage = [[NSString alloc] initWithString:message.text];
    if([ename isEqualToString:@""] && [ephone isEqualToString:@""] && [eemail isEqualToString:@""] && [emessage isEqualToString:@""] ) 
    {
        mylabel.text = @"enter all the field";
        alert = [[UIAlertView alloc] initWithTitle:@"Missing Fields" message:@"Enter all fields" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
    else if ([ename isEqualToString:@""])
    {
        alert = [[UIAlertView alloc] initWithTitle:@"Missing Fields" message:@"Must Enter your name" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
    else if ([ephone isEqualToString:@""])
    {
        alert = [[UIAlertView alloc] initWithTitle:@"Missing Fields" message:@"Must Enter your Phone" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
    else if ([eemail isEqualToString:@""] )
    {
        alert = [[UIAlertView alloc] initWithTitle:@"Missing Fields" message:@"Must Enter your email" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
    else if ([emessage isEqualToString:@""] || emessage.length < 30)
    {
        alert = [[UIAlertView alloc] initWithTitle:@"Missing Fields" message:@"your message lenght not less then 100" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
    else 
    {
        mylabel.text = @"login success";
    }

    [ename release];
    [ephone release];
    [eemail release];
    [emessage release];
    [alert release];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range 
 replacementText:(NSString *)text
{
    // Any new character added is passed in as the "text" parameter
    if ([text isEqualToString:@"\n"]) {
        // Be sure to test for equality using the "isEqualToString" message
        [textView resignFirstResponder];

        // Return FALSE so that the final '\n' character doesn't get added
        return FALSE;
    }
    // For any other character return TRUE so that the text gets added to the view
    return TRUE;
}

- (IBAction)textFieldShouldReturn:(id) sender
{
    [name resignFirstResponder];
    [phone_no resignFirstResponder];
    [email resignFirstResponder];
}

- (IBAction)fields:(id) sender
{
    [sender resignFirstResponder];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
@END

2 个答案:

答案 0 :(得分:1)

您好,这里有用于创建自定义表单的框架工作,您可以根据自己的要求自定义以下是自定义表单创建的链接。

1)QuickDialog

2)IBAForms

愿这对发展有所帮助。

答案 1 :(得分:1)

    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;
    [picker setToRecipients:[In Array your email Id]]; // to add your default email id.

    NSString *emailBody = @"add your text in emailBody";  
    [picker setMessageBody:emailBody isHTML:YES];
    [self presentModalViewController:picker animated:YES];
    [picker release];

完成所有条件检查后,您可以使用此代码打开MFMailComposeViewController 并且不要忘记在头文件中委托MFMailComposeViewController。