2012-03-08 37 views
29

được cập nhật UIAlertView bây giờ có một phong cách cho phép một lĩnh vực Nhập văn bản trong UIAlertView tứcLàm thế nào tôi có thể init một lĩnh vực văn bản trong một UIAlertView

alert.alertViewStyle = UIAlertViewStylePlainTextInput; 

này hoạt động tốt nhưng tôi muốn init văn bản đầu vào với một defualt văn bản như "mẫu".

Tôi thấy rằng folks trong quá khứ đã sử dụng api không có giấy tờ tương tự (trong đó hoạt động tốt)

[alert addTextFieldWithValue:@"sample text" label:@"Text Field"]; 

nhưng vì đây vẫn không phải là một quan chức của Apple API công cộng Tôi không thể sử dụng nó.

Bất kỳ cách nào khác để xử lý việc này? Tôi đã cố gắng bắt đầu trong willPresentAlertView nhưng trường văn bản dường như chỉ đọc.

Cảm ơn

Trả lời

8

Không thử nghiệm, nhưng tôi cho rằng điều này sẽ làm việc:

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:...]; 
UITextField* textField = [alert textFieldAtIndex:0]; 
textField.text = @"sample"; 
[alert show]; 
57

Các UIALertView có một phương pháp textFieldAtIndex: mà trả về đối tượng UITextField bạn muốn.

Đối với một UIAlertViewStylePlainTextInput, chỉ số của textfield là 0.

Sau đó bạn có thể thiết lập các placeholder (hoặc văn bản) tài sản của textfield:

UIAlertView *alert = .... 
UITextField *textField = [alert textFieldAtIndex:0]; 
textField.placeholder = @"your text"; 

UIAlertView Class Reference

+0

vĩ đại - cảm ơn bạn mà làm việc – timeview

+1

Bạn đang chào đón :) Nếu nó đã trả lời câu hỏi của bạn, bạn có thể chấp nhận câu trả lời bằng cách nhấp vào dấu chọn bên cạnh với nó – Mutix

+0

Điều này dường như không hoạt động nữa. textFieldAtIndex tạo ra lỗi này: *** Chấm dứt ứng dụng do ngoại lệ chưa được nắm bắt 'NSInvalidArgumentException', lý do: 'textFieldIndex (0) nằm ngoài giới hạn của mảng các trường văn bản'. – Symmetric

6

Đối với thiết lập mặc định giá trị trong uiAlertView điều này sẽ làm việc.

UIAlertView *alert = .... 
UITextField *textField = [alert textFieldAtIndex:0]; 
[textField setText:@"My default text"]; 
[alert show]; 
0
- (IBAction)showMessage:(id)sender { 
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Add New Category" 
                 message:nil 
                delegate:self 
              cancelButtonTitle:@"Add" 
              otherButtonTitles:@"Cancel", nil]; 
    [message setAlertViewStyle:UIAlertViewStylePlainTextInput]; 
    [message show]; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; 
    if([title isEqualToString:@"Add"]) 
    { 
     UITextField *username = [alertView textFieldAtIndex:0]; 
     NSLog(@"Category Name: %@", username.text); 
    } 
} 
9

Cách dễ dàng để làm

UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"your title" message:@"your message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 
    alerView.alertViewStyle = UIAlertViewStylePlainTextInput; 
    [[alerView textFieldAtIndex:0] setPlaceholder:@"placeholder text..."]; 
    [alerView show]; 
+0

Câu trả lời hoàn hảo :) –

Các vấn đề liên quan