2009-05-07 53 views
7

Có thể hiển thị cảnh báo với hộp văn bản bên trong như ứng dụng AppStore hay không. Nó yêu cầu mật khẩu trong hộp thoại như vậy. Tôi đã nhìn thấy ít nhất một số ứng dụng của bên thứ ba khác sử dụng nó. Đây có phải là API riêng tư không?Hiển thị cảnh báo với hộp văn bản trong iPhone

Trả lời

9

Có, nó không có giấy tờ. Để thêm một trường văn bản vào UIAlertView, sử dụng phương thức addTextFieldWithValue: label:. Bạn gọi với văn bản mặc định làm đối số đầu tiên và văn bản hiển thị trong trường trống làm trường thứ hai. Sau khi thực hiện điều đó, bạn có thể truy cập trường thông qua việc sử dụng textFieldAtIndex: n - xem bên dưới.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Who are you?"  
         message:@"Give your full name" 
         delegate:self cancelButtonTitle:@"Cancel" 
         otherButtonTitles:@"OK", nil]; 
[alert addTextFieldWithValue:@""label:@"Name"]; 

// Customise name field 
UITextField* name = [alert textFieldAtIndex:0]; 
name.clearButtonMode = UITextFieldViewModeWhileEditing; 
name.keyboardType = UIKeyboardTypeAlphabet; 
name.keyboardAppearance = UIKeyboardAppearanceAlert; 
[alert show]; 

Đoạn tiếp theo cho thấy làm thế nào để lấy giá trị trong lĩnh vực tên:

NSLog("Name is %@", [[modalView textFieldAtIndex:0] text]); 
+5

Như những người khác đã đề cập, điều này sẽ giúp bạn khởi động từ cửa hàng ứng dụng hiện nay. – zoul

+0

Đồng ý - điều này đã được viết một thời gian dài trước đây. Tôi có nên xóa câu trả lời không. –

+0

@JaneSales: Tôi sẽ không xóa câu trả lời, nhưng tôi chắc chắn sẽ thêm tuyên bố từ chối trách nhiệm lớn, đáng chú ý ở đầu bài đăng của bạn. – FreeAsInBeer

3

Jeff Lamarche đăng một số sample code on his blog làm chỉ này. Định dạng trông có vẻ hơi sôi nổi khi tôi thử nó nhưng nó có thể là một điểm khởi đầu tốt.

16

Dưới đây là cách "Apple phê duyệt" để thực hiện điều đó từ Tharindu Madushana. Tôi đã nhận nó từ bình luận của mình trong trang này: http://iosdevelopertips.com/undocumented/alert-with-textfields.html

// Ask for Username and password. 
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Twitter Details!" message:@"\n \n \n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 

// Adds a username Field 
UITextField *utextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)]; 
utextfield.placeholder = @"Username"; 
[utextfield setBackgroundColor:[UIColor whiteColor]]; 
[alertview addSubview:utextfield]; 

// Adds a password Field 
UITextField *ptextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 80.0, 260.0, 25.0)]; 
ptextfield.placeholder = @"Password"; 
[ptextfield setSecureTextEntry:YES]; 

[ptextfield setBackgroundColor:[UIColor whiteColor]]; [alertview addSubview:ptextfield]; 
// Move a little to show up the keyboard 
CGAffineTransform transform = CGAffineTransformMakeTranslation(0.0, 80.0); 
[alertview setTransform:transform]; 

// Show alert on screen. 
[alertview show]; 
[alertview release]; 

//... 
// Don't forget to release these after getting their values 
[utextfield release]; 
[ptextfield release]; 

Và cuối cùng để có được văn bản trở lại

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 0) 
     return; //Cancel 

    UITextField *field = (UITextField *)[[alertView subviews] lastObject]; 
    NSLog (@"%@", field.text); 
} 
+0

Cá nhân tôi sẽ xóa biến đổi và thêm [utextfield trở thànhFirstResponder] sau khi bạn phát hành chế độ xem cảnh báo để bàn phím tự động bật lên. – AlBeebe

5

Đây là một câu hỏi thực sự cũ với câu trả lời thực sự cũ.

Đây là một mẫu như thế nào tôi nhận được một UITextField thành một UIAlertView từ ios 5:

UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"New List Name" message:@"" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Continue", nil]; 

    message.alertViewStyle = UIAlertViewStylePlainTextInput; 
    self.alertTextField = [message textFieldAtIndex:0]; 
    self.alertTextField.keyboardType = UIKeyboardTypeAlphabet; 
    message.delegate = self; 
    [message show]; 
    [self.alertTextField becomeFirstResponder]; 

nơi alertTextField được thành lập như thế này:

@property (nonatomic, strong) UITextField *alertTextField; 
Các vấn đề liên quan