iOS NSArray类方法

时间:2014-07-01 14:02:57

标签: ios objective-c

我在创建一个带有Text Fields字符串的NSArray类方法时遇到了麻烦,可以在另一个视图控制器类中使用。

让我告诉你我做了什么:

首先,在设置视图控制器中,我在3个文本字段中收集信息并使用NSUserdefaults进行保存:

- (void)viewDidLoad
{
    // Get the stored data before the view loads
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSString *toEmail = [defaults objectForKey:@"toEmail"];
    NSString *ccEmail = [defaults objectForKey:@"ccEmail"];
    NSString *bccEmail = [defaults objectForKey:@"bccEmail"];

    // Update the UI elements with the saved data
    self.toEmailTextField.text = toEmail;
    self.ccEmailTextField.text = ccEmail;
    self.bccEmailTextField.text = bccEmail;

    [super viewDidLoad];

    [self sideBarButton];

    [self dismissTextFields];

}


- (IBAction)toEmailAction:(id)sender {

    NSString *toEmail = self.toEmailTextField.text;

    // Store the data
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setObject:toEmail forKey:@"toEmail"];

    [defaults synchronize];

    NSLog(@"Data saved");


}
- (IBAction)ccEmailAction:(id)sender {

    NSString *ccEmail = self.ccEmailTextField.text;

    // Store the data
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setObject:ccEmail forKey:@"ccEmail"];

    [defaults synchronize];

    NSLog(@"Data saved");
}
- (IBAction)bccEmailAction:(id)sender {

    NSString *bccEmail = self.bccEmailTextField.text;

    // Store the data
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setObject:bccEmail forKey:@"bccEmail"];

    [defaults synchronize];

    NSLog(@"Data saved");
}

其次我有另一个带有电子邮件API的视图控制器,我需要在设置视图控制器中使用NSUserdefault保存的字符串。(标记为文本1,文本2和文本3)

#pragma mark - Email

- (IBAction)showEmail:(id)sender {

    // Email Subject
    NSString *emailTitle = @"GliLog Email";
    // Email Content
    NSString *messageBody = @"GliLog Email Test!!!";
    // To address
    NSArray *toRecipent = [NSArray arrayWithObject:@"text 1"];
    NSArray *ccRecipient = [NSArray arrayWithObject:@"text 2"];
    NSArray *bccRecipient = [NSArray arrayWithObject:@"text 3"];

    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
    mc.mailComposeDelegate = self;
    [mc setSubject:emailTitle];
    [mc setMessageBody:messageBody isHTML:NO];
    [mc setToRecipients:toRecipent];
    [mc setCcRecipients:ccRecipient];
    [mc setBccRecipients:bccRecipient];

    // Present mail view controller on screen
    [self presentViewController:mc animated:YES completion:NULL];

}

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail sent failure: %@", [error localizedDescription]);
            break;
        default:
            break;
    }

    // Close the Mail Interface
    [self dismissViewControllerAnimated:YES completion:NULL];
}

我怎样才能通过"从设置视图控制器到另一个视图的字符串。

祝你好运

2 个答案:

答案 0 :(得分:0)

如果您要将数据存储在NSUserDefaults中,只需从中获取值。

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
 NSString * toEmail = [defaults objectForKey:@"toEmail"];
 NSString * ccEmail = [defaults objectForKey:@"ccEmail"];

关于NSUserDefaults

的简单教程

答案 1 :(得分:0)

在阅读NSUserDefaults并存储到字符串时,您的第一个视图中已经有了答案:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSString *toEmail = [defaults objectForKey:@"toEmail"];
NSString *ccEmail = [defaults objectForKey:@"ccEmail"];
NSString *bccEmail = [defaults objectForKey:@"bccEmail"];

只需在第二个视图控制器中应用相同的内容,

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSArray *toRecipent = [NSArray arrayWithObject:[defaults objectForKey:@"toEmail"]];
NSArray *ccRecipient = [NSArray arrayWithObject:[defaults objectForKey:@"ccEmail"]];
NSArray *bccRecipient = [NSArray arrayWithObject:[defaults objectForKey:@"bccEmail"]];