Tôi cần thêm TextField
vào UIAlertView
. Tôi hiểu rằng táo không khuyến khích cách tiếp cận này. Vì vậy, có bất kỳ thư viện mà tôi có thể sử dụng để thêm một TextField
vào một khung nhìn giống nhau không?Thêm TextField vào UIAlertView
Trả lời
Hãy thử một cái gì đó như thế này:
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Title"
message:@"\n\n"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Save", nil] autorelease];
CGRect rect = {12, 60, 260, 25};
UITextField *dirField = [[[UITextField alloc] initWithFrame:rect] autorelease];
dirField.backgroundColor = [UIColor whiteColor];
[dirField becomeFirstResponder];
[alert addSubview:dirField];
[alert show];
Bạn có thể thử:
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Your title here!" message:@"this gets covered" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[myAlertView addSubview:testTextField];
[myAlertView show];
[myAlertView release];
theo link này để xem chi tiết.
Có, mã của bạn hoạt động. Nhưng Apple không khuyến khích cách tiếp cận này, vì nó sử dụng các API riêng. SO là có cách nào khác tôi có thể giải quyết này? – shajem
Không có API riêng được sử dụng ở đây .. –
@anonymous OP đề cập đến câu đó từ các tài liệu: Phân cấp khung nhìn cho lớp này là riêng tư và không được sửa đổi. –
hãy tham khảo ... http://iosdevelopertips.com/undocumented/alert-with-textfields.htmlđây là api riêng tư và nếu bạn sử dụng ứng dụng trong appstore, ứng dụng có thể bị từ chối nhưng tốt cho phát triển doanh nghiệp.
Cảm ơn bạn đã trả lời, nhưng tôi hy vọng sẽ xuất bản ứng dụng của tôi lên appstore: S – shajem
hơn tôi sẽ không khuyên bạn nên thực hiện việc này. –
Tôi đang sử dụng BlockAlertsAndActionSheets thay vì các thành phần của Apple cho AlertViews và ActionSheets khi tôi thích phương pháp chặn. Cũng chứa một số BlockTextPromptAlertView
trong nguồn, có thể là những gì bạn muốn. Bạn có thể thay thế hình ảnh của điều khiển đó để lấy lại phong cách Apple.
Tutorial which gets you started
Ví dụ:
- (IBAction)newFolder:(id)sender {
id selfDelegate = self;
UITextField *textField;
BlockTextPromptAlertView *alert = [BlockTextPromptAlertView promptWithTitle :@"New Folder"
message :@"Please enter the name of the new folder!"
textField :&textField];
[alert setCancelButtonWithTitle:@"Cancel" block:nil];
[alert addButtonWithTitle:@"Okay" block:^{
[selfDelegate createFolder:textField.text];
}];
[alert show];
}
- (void)createFolder:(NSString*)folderName {
// do stuff
}
Bạn có thể chỉ cho tôi một ví dụ sử dụng 'BlockTextPromptAlertView'. Tôi theo ấn tượng rằng các nhà phát triển không được phép thêm bất kỳ textfields vv để UIAlertView. – shajem
Bạn phù hợp với ấn tượng của mình. Đó là lý do tại sao BlockAlertsAndActionSheets là ** không ** sử dụng UIAlertView, nó hoàn toàn độc lập khi không sử dụng bất kỳ API riêng tư nào. Tôi không có quyền truy cập vào các nguồn của mình ngay bây giờ, nhưng có thể cập nhật câu trả lời của tôi bằng mẫu mã trong vài giờ. –
được cập nhật với cách sử dụng ví dụ –
Thật không may là API chính thức duy nhất cho điều này là iOS 5 trở lên, đó là một tài sản được gọi alertViewStyle
có thể được thiết lập để các thông số sau:
UIAlertViewStyleDefault
UIAlertViewStyleSecureTextInput
UIAlertViewStylePlainTextInput
UIAlertViewStyleLoginAndPasswordInput
UIAlertViewStylePlainTextInput
là thứ bạn muốn.
Lừa đảo với phân cấp chế độ xem như được mô tả ở trên không được Apple khuyến khích mạnh mẽ.
Tôi phải tạo điều này cho iOS 4 và 5. Vì vậy, bạn có biết bất kỳ thư viện nào khác mà tôi có thể sử dụng – shajem
Đối với iOS5:
UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Title" message:@"Please enter someth" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
av.alertViewStyle = UIAlertViewStylePlainTextInput;
[av textFieldAtIndex:0].delegate = self;
[av show];
Ngoài ra, bạn sẽ cần phải thực hiện UITextFieldDelegate
, UIAlertViewDelegate
giao thức.
thêm để trả lời từ 'Shmidt', mã để chụp văn bản nhập vào UIAlertView được dán bên dưới (cảm ơn bạn 'Wayne Hartman' Getting text from UIAlertView)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
self.userNumber = [alertView textFieldAtIndex:0].text;
if (self.userNumber) {
// user enetered value
NSLog(@"self.userNumber: %@",self.userNumber);
} else {
NSLog(@"null");
}
}
}
Tính đến iOS 8 UIAlertView
đã phản đối ủng hộ UIAlertController
, có thêm hỗ trợ cho việc thêm UITextField
s sử dụng phương pháp này:
- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler;
Xem this answer cho một ví dụ.
trước hết Thêm UIAlertViewDelegate vào ViewController.h tập tin như
#import <UIKit/UIKit.h>
@interface UIViewController : UITableViewController<UIAlertViewDelegate>
@end
và hơn Add Dưới Mã nơi bạn muốn để cảnh báo hiển thị,
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title"
message:@"Message"
delegate:self
cancelButtonTitle:@"Done"
otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
và phương pháp đại biểu của nó mà trả về những gì đầu vào của UITextField
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"%@", [alertView textFieldAtIndex:0].text);
}
- 1. AutoCapitalization của TextField của UIAlertView
- 2. iphone-sdk: Thêm trường văn bản vào UIAlertview không hoạt động trong iOS 4?
- 3. Django Admin: Thêm TinyMCE vào TextField cụ thể chỉ?
- 4. Thực hiện tìm kiếm textField vào jTable
- 5. Cách thêm liên kết Url có thể chạm vào url trong thông điệp của UIAlertView?
- 6. xác thực iphone cho trường văn bản được thêm vào trong uialertview
- 7. Không thể thêm UITextField vào UIAlertView trên iOS7 ... hoạt động trong iOS 6
- 8. Chặn UIAlertView
- 9. UIAlertView Delegates
- 10. UIAlertView cancelButtonTitle
- 11. AS3 TextField autoscroll xuống đáy
- 12. UIAlertView sự kiện Click bên UIAlertView đại biểu
- 13. Hành động nút UIAlertView?
- 14. Thay thế UIAlertView
- 15. Lấy văn bản từ UIAlertView
- 16. Ẩn UIAlertView theo chương trình?
- 17. ngăn UIAlertView loại bỏ
- 18. UIView Popup như UIAlertView
- 19. Tuỳ chỉnh UIAlertView
- 20. Django CharField vs TextField
- 21. otherButtonTiêu đề trong UIAlertView
- 22. Làm thế nào để tạo sự khác biệt giữa textField.setText() và thêm văn bản vào textField bằng tay trong java?
- 23. UIAlertView: UIAlertViewStyleSecureTextInput: Numeric Bàn phím
- 24. UIAlertView thực sự trong iPhone?
- 25. Sử dụng UIAlertView trong NSObject
- 26. JavaFX 2.2 FXML Validated TextField
- 27. hủy UIAlertView theo lập trình
- 28. Django - Quản trị: list_display TextField
- 29. EXC_BAD_ACCESS trên mã UIAlertview = 1
- 30. TextField mất tích trong django.forms
tôi nghĩ rằng Apple không khuyến khích của bạn tiếp cận. – shajem