2013-08-02 34 views
0

如圖我被添加的UITextField到UIAlertView中:UIAlertView中與的UITextField

UIAlertView *addingBookAlert=[[UIAlertView alloc]initWithTitle:@"Enter Note" message:NULL delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 
    addingBookAlert.tag=kAddNoteAlertTag; 

    myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)]; 
    [email protected]"Book Mark"; 
    [myTextField becomeFirstResponder]; 
    [myTextField setBackgroundColor:[UIColor whiteColor]]; 
    myTextField.textAlignment=UITextAlignmentCenter; 
    myTextField.delegate=self; 
    CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, 60); 
    [addingBookAlert setTransform:myTransform]; 
    [addingBookAlert addSubview:myTextField]; 
    [addingBookAlert show]; 

我肖像其顯示具有文本框,取消和OK button.But在景觀其僅出現文本框,取消和OK按鈕並未顯示。

如何解決這個... 先謝謝了。

回答

2

使用alertview這樣

UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Enter Name" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Submit", nil]; 
    alert.alertViewStyle=UIAlertViewStylePlainTextInput; 
    UITextField *textField=[alert textFieldAtIndex:0]; 
    textField.delegate=self; 
    [alert show]; 
    [alert release]; 

和訪問文本字段的值,這樣

#pragma mark alert button view button 
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 

    if(buttonIndex==1) 
    { 
     UITextField *textField=[alertView textFieldAtIndex:0]; 
} 
} 
0

在iOS系統中8 UIAlertView中已被棄用。 UIAlertController來代替這一點。

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Add Fruit" message:@"Type to add a fruit" preferredStyle:UIAlertControllerStyleAlert]; 
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField){ 
     textField.placeholder = @"Fruit"; 
}]; 

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" 
                style:UIAlertActionStyleDefault 
               handler:^(UIAlertAction *action) { 
                UITextField *textFiel = [alert.textFields firstObject]; 
                [fruits addObject:textFiel.text]; 
               }]; 
[alert addAction:okAction]; 

[self presentViewController:alert animated:YES completion:nil]; 
相關問題