Reputation: 629
I have a ViewControllerA and two custom alerts and I'm calling the first custom alert on the buttonclick action of the ViewControllerA.
In first custom alert I'm having a text field and a button.
When I click on the button action of first custom alert , I want to open the second custom alert and both alerts I want show on ViewControllerA.
The expected result is to show both the custom alerts on the single ViewControllerA
Please help me out to solve this problem
class ViewControllerA : UIViewController{
@IBAction func ViewControllerAButtonTapped(_ sender: Any) {
FirstCustomalert.instance.ShowAlert()
self.dismiss(animated: true, completion: nil)
}
}
class FirstCustomAlert : UIView{
@IBOutlet var parentView: UIView!
static let instance = FirstCustomAlert()
@IBOutlet weak var firstTextField: UITextField!
override init(frame: CGRect) {
super.init(frame: frame)
Bundle.main.loadNibNamed("FirstCustomAlert", owner: self, options: nil)
CommonInit()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func CommonInit(){
alertView.frame = CGRect(x: center.x, y: center.y, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
ParentView.autoresizingMask = [ .flexibleHeight , .flexibleWidth ]
}
func ShowAlert(){
UIApplication.shared.keyWindow?.addSubview(ParentView)
}
@IBAction func SubmitBittonTapped(_ sender: Any) {
SecondCustomAlert.instances.ShowAlerts()
}
}
class SecondCustomAlert : UIView{
@IBOutlet var parentView: UIView!
@IBOutlet weak var secondTextField: UITextField!
static let instances = SecondCustomAlert()
override init(frame: CGRect) {
super.init(frame: frame)
Bundle.main.loadNibNamed("SecondCustomAlert", owner: self, options: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func CommonInit(){
parentView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
parentView.autoresizingMask = [.flexibleWidth , .flexibleHeight ]
}
func ShowAlerts(){
UIApplication.shared.keyWindow?.addSubview(parentView)
}
}
Upvotes: 0
Views: 163
Reputation: 1504
Firstly, there are a number of things you need to change:
Your methods should be named with lowerCamelCase and only classes and objects should be CamelCase.
ViewController
and use present(viewController: animated:)
with presentationStyle
of .overCurrentContext
. You can get the topViewController
on the keyWindow
which you have and call the present
function. Then in your AlertViewController
you can do separate animations to display the second view of the alert upon button click. What you are trying to do now is put too much logic into a UIView
, which is not best practise and it isn't very scalable. Plus it looks messy. You are trying to achieve something relatively simply but the current approach already creates 2 customs views and you are already encountering callback issues.
ADD:
class ViewControllerA : UIViewController{
@IBAction func ViewControllerAButtonTapped(_ sender: Any) {
let customAlert = CustomAlertViewController()
customAlert.modalPresentationStyle = .overCurrentContext
self.present(customAlert, animated: true, completion: nil)
}
}
Upvotes: 3