Không thể tìm thấy lời giải thích rõ ràng và mang tính thông tin về điều này.Tìm giải thích rõ ràng về cảnh báo nhanh (UIAlertController) ở đâu?
Trả lời
Sau khi tìm kiếm một thời gian về một chủ đề tôi đã không tìm một lời giải thích rõ ràng, ngay cả trong tài liệu tham khảo lớp nó UIAlertController Reference
Nó là ok, nhưng không đủ rõ ràng đối với tôi.
Vì vậy, sau khi thu thập một số peaces tôi quyết định làm cho lời giải thích của riêng tôi (Hy vọng nó giúp)
Vì vậy, ở đây nó đi:
UIAlertView
bị phản đối như đã chỉ ra: UIAlertView in SwiftUIAlertController
nên được sử dụng trong iOS8 + để tạo một cái đầu tiên, chúng ta cần phải khởi tạo nó, Constructor (init) lấy 3 tham số:
2.1 tiêu đề: String -> văn bản lớn đậm để hiển thị trên đỉnh của hộp thoại cảnh báo của
2.2 nhắn: String -> văn bản nhỏ hơn (khá nhiều giải thích nó tự)
2,3 prefferedStyle:UIAlertControllerStyle
- > xác định kiểu hộp thoại, trong hầu hết các trường hợp: UIAlertControllerStyle.Alert
Bây giờ để thực hiện điều đó cho người sử dụng, chúng ta có thể sử dụng
showViewController
hoặcpresentViewController
và vượt qua cảnh báo của chúng tôi như tham sốĐể thêm một số tương tác với người dùng chúng ta có thể sử dụng:
4,1 UIAlertController.addAction
để tạo các nút
4.2 UIAlertController.addTextField
để tạo ra các lĩnh vực văn bản
Sửa lưu ý: đang ví dụ dưới đây, được cập nhật trong vòng 3 cú pháp nhanh chóng
Ví dụ 1: Simple Dialog
@IBAction func alert1(sender: UIButton) {
//simple alert dialog
let alert=UIAlertController(title: "Alert 1", message: "One has won", preferredStyle: UIAlertControllerStyle.alert);
//show it
show(alert, sender: self);
}
Ví dụ 2: Dialog với một văn bản đầu vàoField & hai nút
@IBAction func alert2(sender: UIButton) {
//Dialog with one input textField & two buttons
let alert=UIAlertController(title: "Alert 2", message: "Two will win too", preferredStyle: UIAlertControllerStyle.alert);
//default input textField (no configuration...)
alert.addTextField(configurationHandler: nil);
//no event handler (just close dialog box)
alert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.cancel, handler: nil));
//event handler with closure
alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler: {(action:UIAlertAction) in
let fields = alert.textFields!;
print("Yes we can: "+fields[0].text!);
}));
present(alert, animated: true, completion: nil);
}
Ví dụ 3: Một tùy TextField đầu vào & một nút
@IBAction func alert3(sender: UIButton) {
// one input & one button
let alert=UIAlertController(title: "Alert 3", message: "Three will set me free", preferredStyle: UIAlertControllerStyle.alert);
//configured input textField
var field:UITextField?;// operator ? because it's been initialized later
alert.addTextField(configurationHandler:{(input:UITextField)in
input.placeholder="I am displayed, when there is no value ;-)";
input.clearButtonMode=UITextFieldViewMode.whileEditing;
field=input;//assign to outside variable(for later reference)
});
//alert3 yesHandler -> defined in the same scope with alert, and passed as event handler later
func yesHandler(actionTarget: UIAlertAction){
print("YES -> !!");
//print text from 'field' which refer to relevant input now
print(field!.text!);//operator ! because it's Optional here
}
//event handler with predefined function
alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler: yesHandler));
present(alert, animated: true, completion: nil);
}
Hy vọng Nó giúp, và may mắn ;-)
Tôi vui vì bạn đã chia sẻ, những gì bạn đã học được !! Cảm ơn bạn rất nhiều vì đã giải thích từng bước ..) – swiftBoy
http://iosdevcenters.blogspot.in/2016/03/uialertcontroller-in-swift.html –
Tôi đang chạy trên Xcode 7.3.1 và thử nghiệm trên iOS9 . Khi sử dụng showViewController (cảnh báo, người gửi: self) từ ví dụ đầu tiên với UINavigationController (không có nó ok) Tôi đã nhận được hành vi kỳ lạ. Nó thêm chế độ xem cảnh báo vào thanh điều hướng. Sử dụng self.presentViewController (alert, animated: true, completion: nil) hoạt động tốt. – pls
Một ví dụ của UIAlertController thể được trình bày modally trên màn hình giống như bất kỳ UIViewController nào khác sử dụng phương thức presentViewController: animated: completion:. Điều gì làm cho cá thể UIAlertController phân biệt giữa làm việc như một ActionSheet hoặc như một AlertView là tham số kiểu mà bạn truyền khi tạo nó.
Không đoàn hơn
Nếu bạn đã sử dụng một UIActionSheet hoặc UIAlertView, bạn biết rằng cách thức để có được một cuộc gọi lại từ nó là dành cho một lớp (trong hầu hết trường hợp, các ViewController) để thực hiện giao thức UIActionSheetDelegate hoặc UIAlertViewDelegate. Có một số dự án mã nguồn mở thay thế mẫu ủy quyền này bằng các cuộc gọi lại dựa trên khối, nhưng các API chính thức chưa bao giờ được cập nhật. UIAlertController không sử dụng ủy quyền. Thay vào đó, nó có một tập hợp các mục UIAlertAction, sử dụng các bao đóng (hoặc khối nếu bạn đang sử dụng Objective-C) để xử lý đầu vào của người dùng.
Đối với hành động Tấm
@IBAction func showActionSheet(sender: AnyObject) {
//Create the AlertController
let actionSheetController: UIAlertController = UIAlertController(title: "Action Sheet", message: "Swiftly Now! Choose an option!", preferredStyle: .ActionSheet)
//Create and add the Cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
//Just dismiss the action sheet
}
actionSheetController.addAction(cancelAction)
//Create and add first option action
let takePictureAction: UIAlertAction = UIAlertAction(title: "Take Picture", style: .Default) { action -> Void in
//Code for launching the camera goes here
}
actionSheetController.addAction(takePictureAction)
//Create and add a second option action
let choosePictureAction: UIAlertAction = UIAlertAction(title: "Choose From Camera Roll", style: .Default) { action -> Void in
//Code for picking from camera roll goes here
}
actionSheetController.addAction(choosePictureAction)
//Present the AlertController
self.presentViewController(actionSheetController, animated: true, completion: nil)
}
Đối AlertView với Text Field
@IBAction func showAlert(sender: AnyObject) {
//Create the AlertController
let actionSheetController: UIAlertController = UIAlertController(title: "Alert", message: "Swiftly Now! Choose an option!", preferredStyle: .Alert)
//Create and add the Cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
//Do some stuff
}
actionSheetController.addAction(cancelAction)
//Create and an option action
let nextAction: UIAlertAction = UIAlertAction(title: "Next", style: .Default) { action -> Void in
//Do some other stuff
}
actionSheetController.addAction(nextAction)
//Add a text field
actionSheetController.addTextFieldWithConfigurationHandler { textField -> Void in
//TextField configuration
textField.textColor = UIColor.blueColor()
}
//Present the AlertController
self.presentViewController(actionSheetController, animated: true, completion: nil)
}
Cảm ơn bạn đã đóng góp, thậm chí tôi đã trả lời nó nửa năm trước. Vẫn còn câu trả lời rất tốt đẹp của nó vì vậy tôi sẵn sàng bỏ phiếu ;-) BTW, bạn cũng có thể hiển thị cảnh báo với phương pháp showViewController - ít linh hoạt hơn, nhưng có ý nghĩa khi hoàn thành không cần thiết –
Nên hiển thị cảnh báo yêu cầu đọc nhiều? KHÔNG. – chrisl08
Một số cú pháp đã thay đổi kể từ những phản ứng ban đầu. Dưới đây là một số mã mẫu cảnh báo người dùng nếu họ không đăng nhập vào iCloud.
CKContainer.default().accountStatus { (accountStatus, error) in
switch accountStatus {
case .available:
print("iCloud Available")
case .noAccount:
print("No iCloud account")
//simple alert dialog
let alert=UIAlertController(title: "Sign in to iCloud", message: "This application requires iClound. Sign in to your iCloud account to write records. On the Home screen, launch Settings, tap iCloud, and enter your Apple ID. Turn iCloud Drive on. If you don't have an iCloud account, tap Create a new Apple ID", preferredStyle: UIAlertControllerStyle.alert);
//no event handler (just close dialog box)
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil));
//show it
self.present(alert, animated: true, completion: nil)
case .restricted:
print("iCloud restricted")
case .couldNotDetermine:
print("Unable to determine iCloud status")
}
}
- 1. Tôi có thể đọc giải thích rõ ràng về POE (Môi trường đối tượng Perl) ở đâu?
- 2. Git diff - giải thích rõ ràng
- 3. Tôi có thể tìm các ví dụ rõ ràng về MVC ở đâu?
- 4. Tôi có thể thấy lý do cảnh báo ở đâu?
- 5. Giải thích ngắn/nhanh về .net mvc
- 6. mở rộng cảnh báo UIAlertController tiện init
- 7. không rõ ràng cảnh báo kiểm tra "NullableProblems" trong IntelliJ
- 8. Cách thích hợp để giải quyết sự không rõ ràng về quy tắc lexer ANTLR?
- 9. Giải thích về Func
- 10. Đề xuất về "ràng buộc cấu trúc" ở đâu?
- 11. Chú thích JDBC4 ở đâu?
- 12. Một lời giải thích rõ ràng của giáo dân về sự khác biệt giữa | và || trong C#?
- 13. Tôi có thể tìm hiểu về chuỗi JNDI ở đâu?
- 14. Số cảnh báo Visual Studio 2010. Tôi tìm thấy chúng ở đâu?
- 15. Cần một số giải thích rõ ràng với Patterns (DAO x Gateway)
- 16. Giải thích cơ bản về Bối cảnh trong android
- 17. Ai đó có thể giải thích rõ ràng jQuery.when() và deferred.then() hoạt động như thế nào?
- 18. Giải thích về window.performance javascript
- 19. Có cách nào để nhận được gcc hoặc clang để cảnh báo về phôi rõ ràng không?
- 20. Tên thuộc tính rõ ràng dư thừa trong cảnh báo C#
- 21. Giải thích CanCan về load_and_authorize_resource
- 22. Có ai có thể cung cấp giải thích rõ ràng về lý do khiến Google Guice hữu ích không?
- 23. dispatch_async bị trễ ở đâu đó, không thể tìm thấy ở đâu. có vấn đề về NSLog không?
- 24. Một giải thích rõ ràng hơn cho đệ quy và luồng thực thi trong JavaScript?
- 25. Tôi có thể tìm tài liệu về Hpricot ở đâu?
- 26. Không gian trắng không giải thích được khi sử dụng rõ ràng: cả hai
- 27. Tôi tìm Galasoft.MvvmLight.WPF45 ở đâu?
- 28. Rõ ràng lấy trả về NULL
- 29. Một số giải thích về XSS
- 30. Tôi có thể tìm GACUtil ở đâu?
định dạng Stack Overflow đòi hỏi một câu hỏi. Nếu bạn muốn trả lời câu hỏi của riêng bạn, bạn có thể, nhưng trước tiên bạn phải đặt một câu hỏi. Định dạng lại bài đăng của bạn dưới dạng câu hỏi bạn đang cố gắng tìm câu trả lời và sau đó chuyển bài đăng ở trên của bạn lên câu trả lời. – vacawama
Bạn có thể sử dụng 'UIAlertController' cho cả cảnh báo và trang tính hành động. Tôi tìm thấy các ví dụ cách dễ nhất để hiểu cách mọi thứ hoạt động. Dưới đây là [ví dụ cảnh báo] (https://stackoverflow.com/a/33340757/3681880) và đây là [ví dụ về trang tính] (https://stackoverflow.com/a/32991999/3681880). – Suragch