From alert to ViewController

Hi again​:smile::wave:t2:
I working Alert ok :heart_eyes: but I’m facing a problem :sob::broken_heart:
I have 2 ViewController
-when pressing (Logain)to go HOMEController()
-when pressing (back) to go LoginEmployee()
but don’t working why …??
it is image code👇🏻

What exactly are you trying to do?
Are you trying to set up an alert which has two options? One to go to HomeController and the other to go to LoginEmployee?

Yes but don’t working :broken_heart::expressionless:

Can you show me the code where you call the showBasicAlert() function.

this :pleading_face::heart_eyes:

I still don’t understand what you are trying to do. Are you trying to validate if the user is already registered and if they are then take them to a login screen?

The UIAlertController is a self contained ViewController. It displays on the current view when you call it and you can define more than one button to do something. You don’t need to tell it where to display.

Are you writing most of this program without using storyboard?

ok
Are you writing most of this program without using storyboard?
yes
Are you trying to validate if the user is already registered and if they are then take them to a login screen?
No just when I click on the button it goes to the pages I wanta
after button save data is pressed
‏لقطة الشاشة ١٤٤١-٠٨-١٩ في ٥.٤٧.٤٧ ص

just button save Data and after show alert in button, I want
go to :
1- button go to page (HOMEController)
2- button go to page (LoginEmployess)


OK. If the user data has been recorded then is there a better way of directing the user? Is it necessary to have an alert for them to choose? Could you programatically make a decision to always send them to the HomeScreen? Why would you want to give them a choice to go to the LoginScreen?

What I am thinking is that there are two paths for a user. If they already have a Login then present them with a Login page. If they are a new user then send them to a New user page to record their details as a new user. After they have either Logged In or registered as a new User then send them to the HomeScreen.

Do you understand what I mean.

Have a look at this layout to see how I would structure your project. This was from the Custom Login Demo that Chris Ching created. I know it’s not programmatically written but that’s not hard to do replicate.

Yes yes It’s great structure … I need to be alert about this and other things …

Could you programatically make a decision to always send them to the HomeScreen.??
Yes how …?

And I need That the alert appear in other things and move to the page that was chosen according to the alert button.? How …?

After the user has either:
1: logged in and their login in and password are valid or
2: Registered for access and their details saved to your database
you can write code like this:

For a modal presentation of a ViewController which is a fullscreen presentation you would write:

let destinationVC = HomeViewController()    
destinationVC.modalPresentationStyle = .fullScreen
destinationVC.modalTransitionStyle = .coverVertical
present(destinationVC, animated: true)

for a presentation where a navigation controller is the rootViewController of your current ViewController the process would be:

let destinationVC = HomeViewController()
navigationController?.pushViewController(destinationVC, animated: true)

This also assumes a lot of other things are set in place in your code to create each ViewController.

If you are unfamiliar with programmatic set up, it would be much easier to use storyboard.

Yes thanks
The use of the storyboard is very easy, but I saw many people who do not want to use the storyboard because it is not suitable for many things and warn against using it.
thanks but how use alert Because l wil use alerts on many projects and l want to learn now so that it helps me later
I have two buttons . Once I press the button , it move to a specific page …?
how do l do this I the alert…?

@1112

I would recommend that you don’t believe everything you read on the internet. People who say don’t use storyboard either have not learned how to use storyboard properly or just prefer to write code programatically and then say, “Oh storyboard is no good.”

It is NOT true. Many people use storyboard and the Apps they make are great.

If you know how to use storyboard and set constraints properly it is fine. You just have to take the right approach and add each object to the storyboard in the right order to make it work.

If you place the Alert into an extension like this you may find it easier to use.

import UIKit

extension UIViewController {
    
    func showBasicAlert(with title: String, message: String?) {
        let ac = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let homeAction = UIAlertAction(title: "GO TO HOME", style: .default) { (action) in
            // Using storyboard
//            let destVC = self.storyboard?.instantiateViewController(identifier: "HomeVC") as! HOMEController
//            destVC.modalPresentationStyle = .fullScreen
//            self.present(destVC, animated: true)
            
            // Using Modal presentation (no storyboard)
            let destVC = HOMEController()
            destVC.modalPresentationStyle = .fullScreen
            destVC.modalTransitionStyle = .coverVertical
            self.present(destVC, animated: true)
            
            // Presenting with Navigation Controller as root view controller (no storyboard)
//            let destVC = HOMEController()
//            let navController = UINavigationController(rootViewController: destVC)
//            self.present(navController, animated: true)
        }
        let loginAction = UIAlertAction(title: "GO TO EMPLOYEES", style: .default) { (action) in
            // Using storyboard
//            let destVC = self.storyboard?.instantiateViewController(identifier: "EmpthitVC") as! LoginEmployess
//            destVC.modalPresentationStyle = .fullScreen
//            self.present(destVC, animated: true)
            
            // Using Modal presentation without storyboard
            let destVC = LoginEmployess()
            destVC.modalPresentationStyle = .fullScreen
            destVC.modalTransitionStyle = .coverVertical
            self.present(destVC, animated: true)
            
            // Presenting with Navigation Controller as root view controller
//            let destVC = LoginEmployess()
//            let navController = UINavigationController(rootViewController: destVC)
//            self.present(navController, animated: true)
            
            
        }
        ac.addAction(loginAction)
        ac.addAction(homeAction)
        present(ac, animated: true)
    }
}

By placing it in an extension it makes it available to any ViewController conforming to UIViewController. If you wanted it to only be available to one ViewController then there is no point in it being in an extension anyway so you may as well add the function to the bottom of the ViewController in which you want to use it.

This approach also assumes that the ViewController that this alert is presented in is not one of the destinations in the Alert actions so that implies that the other two are HOMEController and LoginEmployess.

1 Like

NO NO NO NO NO NO YOU GREAT VERY GREAT
thanks ver thank ver ver ver ver thank You really helped me​:sob::sob::heart::heart::heart::two_hearts::two_hearts::two_hearts::two_hearts::two_hearts::two_hearts::two_hearts::two_hearts::two_hearts::two_hearts::two_hearts::two_hearts::two_hearts::dancer:t2::dancer:t2::dancer:t2::dancer:t2::dancer:t2::dancer:t2::heart_eyes::heart_eyes::heart_eyes::heart_eyes::kissing_heart::kissing_heart::kissing_heart::kissing_heart::kissing_heart:

I would recommend that you don’t believe everything you read on the internet. People who say don’t use storyboard either have not learned how to use storyboard properly or just prefer to write code programatically and then say, “Oh storyboard is no good.”
I am really confused about it and don’t know what to do I am confused and do not know what to do, but I believe that I will adopt both in education​:sob::broken_heart: