Why pushviewController does nothing ? Xcode - ios

It is my function I linked to button. It doesn't push to another ViewController. It should work in following way : User clicks button, button creates alert, alert stays in current ViewController or pushes to different existing ViewController. No idea why it is not working - alert shows and nothing happens. Identifier is set and correct.
let alert = UIAlertController(title: "Warning", message: "Aborting adding a routine scheme. Proceed?", preferredStyle: .alert)
alert.addAction(.init(title: "No", style: .cancel, handler: nil))
alert.addAction(.init(title: "Yes", style: .default, handler: { (action) in
alert.dismiss(animated: true, completion: nil)
// push to another VC
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "WorkoutViewController") as! WorkoutViewController
self.navigationController?.pushViewController(vc, animated: true)
}))
self.present(alert, animated: true, completion: nil)

Your current ViewController isn't embed in UINavigationController so this line does nothing
self.navigationController?.pushViewController(vc, animated: true)
... because navigationController is nil
If you want to just present ViewController use this
self.present(vc, animated: true, completion: nil)

Related

programmatically push to a new viewController after pressing uiAlert button

I was wondering if it is possible to push to a newViewController after pressing a button in the UiAlertController
my code looks like
#objc func handleAcceptRequest() {
let user = self.user
let username = user?.name
let alert = UIAlertController(title: "Are you Sure? ",message:" Would you like to accept \(username!) to complete your job?", preferredStyle: UIAlertController.Style.alert)
let cancelButton = UIAlertAction(title: "Cancel", style: .default, handler: {(_ action: UIAlertAction) -> Void in
print("you pressed cancel button")
})
let continueButton = UIAlertAction(title: "Continue", style: .default, handler: {(_ action: UIAlertAction) -> Void in
let vc = viewController()
let navController = UINavigationController(rootViewController: vc)
print("you pressed Continue")
})
continueButton.setValue(GREEN_Theme, forKey: "titleTextColor")
cancelButton.setValue(UIColor.red, forKey: "titleTextColor")
alert.addAction(cancelButton)
alert.addAction(continueButton)
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
}
I would only like to present the VC if the the Continue button is pressed but If I call the present
// self.present(navController, animated: true, completion: nil)
inside of the continueButton, I get the error Use of unresolved identifier 'present'
is it possible to push a newVC this way?
This is not right
// self.present(navController, animated: true, completion: nil)
It should be:
self.window?.rootViewController?. present(navController, animated: true, completion: nil)

Use of unresolved identifier 'present' - the present method doesn't work anymore

I'm going to use UIAlertController but when I want to present it I see this error:
use of unresolved identifier 'present'
This is my code :
func showAllert(title: String, msg: String, vc: UIViewController){
let alert = UIAlertController(title: title, message: msg, preferredStyle: .alert)
let action = UIAlertAction(title: "ok", style: .default, handler: nil)
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
If you want to specifically use vc: UIViewController to present the alert you should call the method this way:
vc.present(alert, animated: true, completion: nil)
The problem is that your presenting view controller name is VC instead of present. If you do vc(alert, animated: true, completion: nil)
It will work.

Navigation does not appear Xcode 8.1 iOS 10 Swift 3

Navigation bar does not appear after login button pressed when it moves to Home view controller and I have setup my storyboard and put all thing well but I think i am stuck at this point in code.
Actually there is no segue but coding instead so How I do navigation working?
Code I used to perform Navigation
let vc = self.storyboard?.instantiateViewController(withIdentifier: "Home")
self.present(vc!, animated: true, completion: nil)
#IBAction func loginAction(_ sender: Any) {
if self.emailTextField.text == "" || self.passwordTextField.text == "" {
//Alert to tell the user that there was an error because they didn't fill anything in the textfields because they didn't fill anything in
let alertController = UIAlertController(title: "Error", message: "Please enter an email and password.", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
} else {
FIRAuth.auth()?.signIn(withEmail: self.emailTextField.text!, password: self.passwordTextField.text!) { (user, error) in
if error == nil {
//Print into the console if successfully logged in
print("You have successfully logged in")
//Go to the HomeViewController if the login is sucessful
let vc = self.storyboard?.instantiateViewController(withIdentifier: "Home")
self.present(vc!, animated: true, completion: nil)
} else {
//Tells the user that there is an error and then gets firebase to tell them the error
let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
}
}
}
Replace your code:
let vc = self.storyboard?.instantiateViewController(withIdentifier: "Home")
self.present(vc!, animated: true, completion: nil)
With this Code:
let vc : YourViewController = self.storyboard?.instantiateViewController(withIdentifier: "Home")as! YorViewController
let navController = UINavigationController(rootViewController: vc)
self.present(navController, animated: true, completion: nil)
You are just trying to present the view controller with out Navigation. Give it Navigation programmatically then it will show Navigation bar.
Are you really using navigationController? If so, should it be pushViewController rather than present?
navigationController?.pushViewController(vc, animated: true)

presentViewController() pops the view behind the last view

I'm trying to perform a transition to another UIViewController in another storyboard but the problem is that the view that I want to display pops behind the view where I'm from.
I know that because my app includes a Snapchat-like navigation between views, so I can drag the view to the left or the right and I can see the view behind.
Here is how I attempt to do that :
#IBAction func account(sender: UIButton) {
if self._isOnline {
self.pageController?.reverseViewControllerAnimated(true)
}
else {
let alertView = UIAlertController(title: "blablablabla", message: "blablablabla", preferredStyle: .Alert)
alertView.addAction(UIAlertAction(title: "No", style: .Cancel, handler: nil))
alertView.addAction(UIAlertAction(title: "Yes", style: .Default, handler: { (alertAction) -> Void in
let storyboard = UIStoryboard(name: "SignUpLogin", bundle: NSBundle.mainBundle())
let vc = storyboard.instantiateInitialViewController() as! UIViewController
self.presentViewController(vc, animated: false, completion: nil)
}))
presentViewController(alertView, animated: true, completion: nil)
}
}
With SignUpLogin that is the name of my second .storyboard file, here I'm in Heart.storyboard.
The action takes place in a UIAlertController, if the user press no, nothing append, if he press yes he's supposed to be redirected to the initial view of my SignUpLogin.storyboard file.
My problem come from the view at the bottom left corner. And if I use the code above in the view at the top right corner, that works ! Why ?
I have no idea of how I can do differently.
I've found the solution to my problem !
#IBAction func account(sender: UIButton) {
if self._isOnline {
self.pageController?.reverseViewControllerAnimated(true)
}
else {
let alertView = UIAlertController(title: Constants.LocalizedJoinOurBand, message: Constants.LocalizedNeedToCreatAnAccount, preferredStyle: .Alert)
alertView.addAction(UIAlertAction(title: Constants.LocalizedNo, style: .Cancel, handler: nil))
alertView.addAction(UIAlertAction(title: Constants.LocalizedYes, style: .Default, handler: { (alertAction) -> Void in
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
let storyboard = UIStoryboard(name: "SignUpLogin", bundle: NSBundle.mainBundle())
let vc = storyboard.instantiateInitialViewController() as! UIViewController
UIApplication.sharedApplication().keyWindow?.rootViewController = vc
}
}))
presentViewController(alertView, animated: true, completion: nil)
}
}
I don't know why in this case particularly but I have to directly set the rootViewController of my AppDelegate with this line :
UIApplication.sharedApplication().keyWindow?.rootViewController = vc

Present a ViewController although UIAlertController is in the view Hierarchy

I want to presentViewController when an alertController is presented.
I want such as if user presses the Ok button now i want to show the ViewController from AppDelegate on PushNotification.
I came to know on SO that UIAlertController is presented on different thread so want to present the ViewController after user presses Ok Button and moves on to main thread as below:
func presentRScreenFromRootViewController()
{
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let rViewController = storyboard.instantiateViewControllerWithIdentifier("rsh") as! R_SVC
dispatch_async(dispatch_get_main_queue(),{
self.window?.rootViewController?.presentViewController(rViewController, animated: true, completion: nil)
});
}
But I get warning as:
Attempt to present <BB.R_SVC: 0x15b983600> on
<SWRevealViewController: 0x15652df00> which is already presenting
<UIAlertController: 0x15b4f2dd0>
This is because the viewcontroller is presenting alertcontroller.Other times its working well..My rootViewController changes and moves to next screen..But when there is UIAlertController i get warning as attempt to present viewcontroller which is not in the view hierarchy
What am i doing wrong?
when creating UIAlertController, place you VCpresenting code in the an UIAlertAction like so
let alert = UIAlertController(title: "your title", message "move to next VC?", preferredStyle: UIAlertController: UIAlertControllerStyle.Alert)
let action: UIAlertAction = UIAlertAction(title: "yes move to next VC", style: UIAlertActionStyle.Default) {action -> Void in
let rViewController = storyboard.instantiateViewControllerWithIdentifier("rsh") as! R_SVC
self.presentViewController(rViewController, animated: true, completion: nil) })
alert.addAction(defaultAction)
self.presentViewController(alert, animated: true, completion: nil)
edit: Maybe try Dismiss all UIAlertControllers currently presented.

Resources