2013-04-02 32 views
0

我有三個UITextfieldsUIAlertView,而水龍頭上一個UITextfield並嘗試挖掘其他的沒有選擇,創造出了問題,也辭職第一響應的問題,是它使用UITextfield不是好選擇UIAlertViewUitextFields在UIAlertView中選擇iOS的

- (IBAction)heightMethod:(id)sender 
{ 
    self.centimeterTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)]; 
    centimeterTextField.placeholder = @" Centimeters"; 
    self.centimeterTextField.delegate=self; 
    self.centimeterTextField.tag=3; 

    [ self.centimeterTextField setBackgroundColor:[UIColor whiteColor]]; 

    [self.alertHeight addSubview: self.centimeterTextField]; 

    self.ptextfield = [[UITextField alloc] initWithFrame:CGRectMake(40, 80.0, 80, 25.0)]; ptextfield.placeholder = @" Feet"; 

    self.ptextfield.delegate=self; 

    self.ptextfield.tag=4; 

    [self.ptextfield setBackgroundColor:[UIColor whiteColor]]; 
    [self.alertHeight addSubview:self.ptextfield]; 
    self.ptextfieldInches = [[UITextField alloc] initWithFrame:CGRectMake(140, 80.0, 80, 25.0)]; ptextfieldInches.placeholder = @" Inches"; 
    self.ptextfieldInches.delegate=self; 
    self.ptextfieldInches.tag=5; 

    [ptextfieldInches setBackgroundColor:[UIColor whiteColor]]; 

    [self.alertHeight addSubview:ptextfieldInches]; 

    [self.centimeterTextField setKeyboardType:UIKeyboardTypeDecimalPad]; 

    [self.ptextfieldInches setKeyboardType:UIKeyboardTypeDecimalPad]; 

    [self.ptextfield setKeyboardType:UIKeyboardTypeDecimalPad]; 

    self.alertHeight.tag=1; 

    [self.alertHeight show]; 
} 
+0

,粘貼代碼 –

+0

是的,請張貼代碼在這裏 – Prashant

+0

使用標籤的正確textfild及其委託檢查與巫Textfild呼籲。或者可能會忘記給三個TextFiled之一的代表進行正確檢查。 –

回答

3
- (void) presentSheet { 
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Enter Information" 
message:@"Specify the Name and URL" delegate:self cancelButtonTitle:@"Cancel"otherButtonTitles:@"OK", nil]; 

[alert addTextFieldWithValue:@"" label:@"Enter Name"]; 
[alert addTextFieldWithValue:@"http://" label:@"Enter URL"]; 

UITextField *tf = [alert textFieldAtIndex:0]; 
tf.clearButtonMode = UITextFieldViewModeWhileEditing; 
tf.keyboardType = UIKeyboardTypeAlphabet; 
tf.keyboardAppearance = UIKeyboardAppearanceAlert; 

tf.autocapitalizationType = UITextAutocapitalizationTypeWords; 
tf.autocorrectionType = UITextAutocorrectionTypeNo; 
// URL field 
tf = [alert textFieldAtIndex:1]; 
tf.clearButtonMode = UITextFieldViewModeWhileEditing; tf.keyboardType = UIKeyboardTypeURL; 
tf.keyboardAppearance = UIKeyboardAppearanceAlert; tf.autocapitalizationType = UITextAutocapitalizationTypeNone; tf.autocorrectionType = UITextAutocorrectionTypeNo; 
[alert show]; 
} 
+1

其不工作,沒有聲明addTextFieldWithValue顯示。 – Youaregreat

+0

@Youaregreat我建議參考iPhone Developer's Cookbook第9頁。沒有115 –

+0

@ouaregreat這不是我開發的這段代碼是從iPhone開發人員的食譜,這是ios初學者真的很好的來源。我個人每天都會提及它。 –

0

現在,您可以創建一個風格UIAlertView中。

UIAlertView中現在有一個屬性 - alertViewStyle,你可以設置爲枚舉值之一

1 UIAlertViewStyleDefault

2. UIAlertViewStyleSecureTextInput

3 UIAlertViewStylePlainTextInput

4. UIAlertViewStyleLoginAndPasswordInput

一旦創建了警報視圖,就可以設置它的樣式並顯示它。解除警報後,可以使用警報視圖的textFieldAtIndex屬性訪問字段的內容。

要不然你也可以使用這一個,

IAlertView* dialog = [[UIAlertView alloc] init]; 
[dialog setDelegate:self]; 
[dialog setTitle:@"Enter Name"]; 
[dialog setMessage:@" "]; 
[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, 70.0); 
[dialog setTransform: moveUp]; 
[dialog show]; 
[dialog release]; 
[nameField release]; 

希望這個信息幫助。謝謝。

+0

其良好的,但對我來說它不夠的,因爲我已經爲米添加了三個文本字段,並轉換成了字,並且在賦值的同時顯示所有的值,我繼續。 – Youaregreat