2015-05-16 17 views
20

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?

+6

đị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

+0

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

Trả lời

61

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:

  1. UIAlertView bị phản đối như đã chỉ ra: UIAlertView in Swift
  2. UIAlertController 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

  1. 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ặc presentViewController và vượt qua cảnh báo của chúng tôi như tham số

  2. Để 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 ;-)

+2

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

+0

http://iosdevcenters.blogspot.in/2016/03/uialertcontroller-in-swift.html –

+0

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

10

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) 
} 
+0

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 –

+0

Nên hiển thị cảnh báo yêu cầu đọc nhiều? KHÔNG. – chrisl08

1

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") 
 
      } 
 
     }

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