সুইফটে একটি ডায়ালগ বক্স তৈরি করতে আমরা UIAlertController ব্যবহার করব যা UIKit-এর একটি গুরুত্বপূর্ণ অংশ৷ আমরা এটি একটি iOS অ্যাপ্লিকেশন এবং একটি নমুনা প্রকল্পের সাহায্যে করব৷
৷প্রথমত, আমরা একটি খালি প্রকল্প তৈরি করব, তারপর তার ডিফল্ট ভিউ কন্ট্রোলারের ভিতরে, আমরা নিম্নলিখিত ক্রিয়াকলাপগুলি সম্পাদন করব৷
আমরা একটি UIAlertController অবজেক্ট তৈরি করব।
let alert = UIAlertController.init(title: title, message: description, preferredStyle: .alert)
আমরা একটি অ্যাকশন তৈরি করব
let okAction = UIAlertAction.init(title: "Ok", style: .default) { _ in
print("You tapped ok")
//custom action here.
} আমরা অ্যালার্টে অ্যাকশন যোগ করব এবং এটি উপস্থাপন করব
alert.addAction(okAction) self.present(alert, animated: true, completion: nil)
এখন আমরা এটিকে একটি ফাংশনে রূপান্তর করব -
func createAlert(withTitle title:String,andDescription description: String) {
let alert = UIAlertController.init(title: title, message: description, preferredStyle: .alert)
let okAction = UIAlertAction.init(title: "Ok", style: .default) {
_ in print("You tapped ok")
//custom action here.
}
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
} আমরা এখন আমাদের viewWillLayoutSubviews পদ্ধতিতে ফাংশনটিকে কল করব, এবং যখন আমরা এটিকে একটি ডিভাইসে চালাই তখন এটি এইরকম দেখায়৷
override func viewWillLayoutSubviews() {
self.createAlert(withTitle: "This is an alert", andDescription: "Enter your description here.")
} এটি নীচে দেখানো হিসাবে ফলাফল তৈরি করে৷
