如何在UIAlertView中允许复制/粘贴/剪切? (iOS,目标C)

时间:2018-08-01 06:16:35

标签: ios objective-c uialertview copy-paste uipasteboard

我想允许用户将其他应用程序中的 PASTE 数据复制/剪切,但主要是复制到我的应用程序中。当他们登录到应用程序时,我需要允许他们将数据粘贴到UIAlertView中。我该怎么做?

这是我的代码:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                message:[NSString stringWithFormat:NSLocalizedString(@"enter_login", nil)]
                                               delegate:self
                                      cancelButtonTitle:@"Ok"


[alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
[[alert textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeEmailAddress];
[[alert textFieldAtIndex:1] becomeFirstResponder];
[UserDefaultsServices resetLoginCredentials];
[UserDefaultsServices resetLoginData];
self.alertView = alert;
[alert show];

它这样做: show what my code does, I need to allow users to paste data in password TextField

1 个答案:

答案 0 :(得分:1)

您可以将this answer用于快速版本:

共享答案的客观C版本为:

- (IBAction)showAlert:(id)sender {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Add New Name" message:@"Your message" preferredStyle:UIAlertControllerStyleAlert];

    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"Name";
    }];

    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"Password";
        textField.secureTextEntry = true;
    }];

    UIAlertAction *saveAction = [UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // here you can access your textfields' texts
        NSString *textField1 = alertController.textFields[0].text;
        NSString *textField2 = alertController.textFields[1].text;
        NSLog(@"Saving %@ %@", textField1, textField2);
    }];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // here you can access your textfields' texts
        NSString *textField1 = alertController.textFields[0].text;
        NSString *textField2 = alertController.textFields[1].text;
        NSLog(@"Canceled %@ %@", textField1, textField2);
    }];

    [alertController addAction:saveAction];
    [alertController addAction:cancelAction];

    [self presentViewController:alertController animated:YES completion:nil];
}

以上代码的工作方式如下:

Copy/Paste Demo