2008-12-17 60 views
23

我放在一個的UITextField到UIAlertView中移動和它使鍵盤不會與下面的代碼覆蓋它:UITextField在iPhone上的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 

我的問題是如何獲取文本用戶輸入和我怎麼dismis鍵盤?

感謝, 本

+3

iPhone OS 4.0似乎自動執行轉換的東西,可能我們將不得不引入一個檢查,如果您的基礎SDK是4.0,並且您將支持的最小SDK是3.0,以便CGAffineTransform代碼僅在本機操作系統是3.0或3.1,爲iPhone OS 4.0跳過這部分代碼! – 2010-07-19 07:23:23

回答

31

對於那些誰可以照顧,這裏是一個有效的解決方案:

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 *名稱字段;在您的.h文件中,然後您可以獲取用戶輸入的文本: inputText = [nameField text];

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

重要注意事項:對於iOS 4.0+,CGAffineTransform是自動完成的,因此如果您只定位到4.0+以上,則可以保留這一點。否則,您將需要檢查您正在使用的操作系統並相應地處理它。

+0

好吧,這工作得很好,但我怎麼讓按鈕向下移動爲文本字段騰出空間?在我的實現中(以你的複製/粘貼開始),我有4個按鈕(Cancel,foo,bar,baz),文本字段覆蓋取消按鈕。 – Olie 2008-12-18 16:42:08

+6

嘿!你爲什麼要保留變量`dialog`?它不是已經分配給你了嗎?我可能是錯的,但看起來你正在以這種方式泄漏一個對象。經驗法則是,如果你分配一個對象,那麼你必須釋放它,因爲它已經有一個保留計數爲1. – inkredibl 2009-05-27 13:59:45

+1

@inkredibl爲了後代,我刪除了導致內存泄漏的保留。 – Epaga 2010-10-24 16:47:20

6

一個簡單的方法來定位文本字段,不保持一個明確提到它,就是設置其標籤:

nameField.tag = ALERTVIEW_NAMEFIELD; 

確保它不爲0,並從你可能有其他UIView對象標記,包括父對話框!

然後alertView:clickedButtonAtIndex:處理程序中,你可以檢索你的文本字段是這樣的:

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

一個相當不錯的方法是,只有當鍵盤被激活時,纔可以使用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; 
} 
49

任何支持的iOS 5。0起弧,有這種直接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]); 
} 
0

這實際上只有1多線到正規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或更高

1

這是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); 
} 
0

想一個小調整增加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+兼容。

相關問題