簡體   English   中英

iPhone上的UIAlertView中的UITextField - 如何使其響應?

[英]UITextField in UIAlertView on iPhone - how to make it responsive?

我將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

我的問題是如何獲取用戶輸入的文本以及如何解除鍵盤?

謝謝,本

任何支持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]);
}

對於那些可能關心的人,這是一個有效的解決方案:

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];

在 - (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex方法。

重要提示:對於iOS 4.0+, CGAffineTransform會自動完成,因此如果您的目標只是4.0+,則可以將其留下。 否則,您需要檢查您所在的操作系統並相應地處理它。

找到文本字段而不保留對其的明確引用的簡單方法是設置其標記:

nameField.tag = ALERTVIEW_NAMEFIELD;

確保它與0和您可能擁有的其他UIView對象標記不同,包括父對話框!

然后在alertView:clickedButtonAtIndex: handler中,您可以通過以下方式檢索文本字段:

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

找到更明確的解決方案 檢查此代碼段: UIAlertView提示中的用戶名和密碼UITextFields

這適用於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);
}

一個非常好的方法是,您可以使用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;
}

想為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+標准。

這實際上只是常規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或更高版本上運行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM