In Swift, managing navigation and modal presentations is achieved primarily through the use of view controllers. By utilizing UINavigationController for navigation and presenting view controllers modally, developers can create seamless and intuitive user experiences. Below are examples of how to handle navigation and modal presentations in a Swift-based application.
// Example of Navigation
class MainViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .system)
button.setTitle("Go to Detail", for: .normal)
button.addTarget(self, action: #selector(navigateToDetail), for: .touchUpInside)
view.addSubview(button)
}
@objc func navigateToDetail() {
let detailVC = DetailViewController()
navigationController?.pushViewController(detailVC, animated: true)
}
}
// Example of Modal Presentation
class DetailViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
let button = UIButton(type: .system)
button.setTitle("Present Modal", for: .normal)
button.addTarget(self, action: #selector(presentModal), for: .touchUpInside)
view.addSubview(button)
}
@objc func presentModal() {
let modalVC = ModalViewController()
modalVC.modalPresentationStyle = .fullScreen
present(modalVC, animated: true, completion: nil)
}
}
class ModalViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .blue
let button = UIButton(type: .system)
button.setTitle("Dismiss", for: .normal)
button.addTarget(self, action: #selector(dismissSelf), for: .touchUpInside)
view.addSubview(button)
}
@objc func dismissSelf() {
dismiss(animated: true, completion: nil)
}
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?