Подтвердить что ты не робот

UITextField в UIAlertView на iPhone - как сделать его отзывчивым?

Я поместил UITextField в UIAlertView и переместил его, чтобы клавиатура не закрыла его следующим кодом:

[dialog setDelegate:self];
[dialog setTitle:@"Enter Name"];
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"OK"];
UITextField * nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:nameField];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 100.0);
[dialog setTransform: moveUp];
[dialog show];

У меня также есть следующий код для просмотра предупреждений:

- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex{    
    NSLog(@"%d", (int) buttonIndex);
    if (buttonIndex == 1) { // OK pushed
        NSLog([alert textField].text); // does not log anything
    } else {
        // Cancel pushed
}}

У меня также есть файл UIAlertViewExtended.h, который содержит:

@class UITextField, UILabel;

@interface UIAlertView(Extended)

-(UITextField *)textField;

@end

Мой вопрос в том, как получить текст, введенный пользователем, и как мне отменить клавиатуру?

Спасибо, Бен

4b9b3361

Ответ 1

Для тех, кто может это ухаживать, здесь работает решение:

UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:@"Enter Name"];
[dialog setMessage:@" "];
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"OK"];

nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:nameField];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 100.0);
[dialog setTransform: moveUp];
[dialog show];
[dialog release];
[nameField release];

Убедитесь, что вы создали UITextField * nameField; в файле .h, то вы можете получить текст, введенный пользователем, выполнив следующие действия: inputText = [nameField text];

в предупреждении alert (alert): (UIAlertView *) clickedButtonAtIndex: (NSInteger) buttonIndex.

важное примечание: для iOS 4.0+, CGAffineTransform выполняется автоматически, поэтому вы можете оставить этот бит, если вы используете только таргетинг 4.0+. В противном случае вам нужно будет проверить, на какой ОС вы находитесь, и обрабатывать его соответствующим образом.

Ответ 2

Любой, поддерживающий iOS 5.0, с ARC, есть это прямое свойство alertViewStyle, которое вы можете установить:

-(void)showAlertWithTextField{
    UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:@"Enter Name" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil];    
    [dialog setAlertViewStyle:UIAlertViewStylePlainTextInput];

    // Change keyboard type
    [[dialog textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
    [dialog show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1)
        NSLog(@"%@",[[alertView textFieldAtIndex:0]text]);
}

Ответ 4

Простым способом поиска текстового поля, не сохраняя явной ссылки на него, является его тег:

nameField.tag = ALERTVIEW_NAMEFIELD;

Убедитесь, что он отличается от 0 и других тегов объектов UIView, которые вы можете иметь, включая родительский диалог!

Затем внутри обработчика alertView:clickedButtonAtIndex: вы можете получить текстовое поле таким образом:

UITextField *nameField = (UITextField *)[alertView viewWithTag:ALERTVIEW_NAMEFIELD];

Ответ 7

Довольно хороший подход: вы можете использовать методы делегата UITextFieldDelegate, чтобы переместить диалог только при активации клавиатуры. См. Ниже.

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5]; 
    [UIView setAnimationDelegate:self];

    CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, -20);
    [dialog setTransform: moveUp];

    [UIView commitAnimations];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5]; 
    [UIView setAnimationDelegate:self];

    CGAffineTransform moveDown = CGAffineTransformMakeTranslation(0.0, 0.0);
    [dialog setTransform: moveDown];

    [textField resignFirstResponder];

    return YES;
}

Ответ 8

Это для iOS5 и ARC.

-(void)showAlert {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"New Album" 
        message:@"Enter a name for the album." 
        delegate:self 
        cancelButtonTitle:@"Cancel" 
        otherButtonTitles:@"Save", nil];
    alertView.alertViewStyle = UIAlertViewStylePlainTextInput;

    UITextField* textfield = [alertView textFieldAtIndex:0];
    textfield.placeholder = @"Title";

    [alertView show];
}

-(void)alertView:(UIAlertView *)alertView 
    didDismissWithButtonIndex:(NSInteger)buttonIndex 
{
    if (buttonIndex != 1) {
        NSLog(@"Cancel");
        return;
    }
    UITextField* textfield = [alertView textFieldAtIndex:0];
    NSLog(@"Save. text: %@", textfield.text);
}

Ответ 9

На самом деле это всего лишь еще одна строка для обычного кода UIAlertView. Надеюсь, это поможет!

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"test" message:@"This is a alert with a textfield" delegate:self cancelButtonTitle:@"YAY" otherButtonTitles:nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [alert show];
    [alert release];

работает только на iOS 5 или более поздней версии

Ответ 10

Хотелось бы добавить одну небольшую настройку к iWasRobbed:

GenericAlertDialogs.m:

+ (void)displayInputDialog:(NSString*)title delegate:(id<UIAlertViewDelegate>)delegate textFiled:(UITextField*)txtField
{
    UIAlertView* dialog = [[UIAlertView alloc] init];
    [dialog setDelegate:delegate];
    [dialog setTitle:title];
    [dialog setMessage:@" "];
    [dialog addButtonWithTitle:@"Cancel"];
    [dialog addButtonWithTitle:@"OK"];

    if(UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]))
    {
        txtField.frame = CGRectMake(20.0, 45.0, 245.0, 25.0);
    }
    else 
    {
        txtField.frame = CGRectMake(20.0, 30.0, 245.0, 25.0);
    }

    [txtField becomeFirstResponder];
    [txtField setBackgroundColor:[UIColor whiteColor]];
    [dialog addSubview:txtField];
    [dialog show];   
}

Другой файл .m(реализует UIAlertViewDelegate)

 self->txtChangeName = [[UITextField alloc] init];
 [GenericAlertDialogs displayInputDialog:@"Some title...:" 
                                delegate:self 
                               textFiled:txtChangeName];

Обработка протокола UIAlertViewDelegate:

- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex 
{
    if(buttonIndex == 1)
    {
        if([txtChangeName.text length] > 0)
        {
            self->userInput = [txtChangeName text];
         }
     }
     self->txtChangeName = nil;
}

iOS 4+.