UIAlertController pushes navigation bar item - swift

UIAlewrtController pushes the button in my navigation controller down (see image)
Sequence of execution:
on push + the alert controller is activated
on selecting "Take an Action" or "Request an Action" the view controller ActionDetialsVC is activated.
on showing ActionDetialsVC
on back the ActionDetialsVC is closed
The iitial controlers BACK arrow moved
When putting breakpoints on:
self.present (alert, animated: true, completion: nil)
the first Swift instruction in the 2 alert handlers
The strange navigation back button behavior is visible between these breakpoints.
The issue ONLY occurs when introducing Alert Controllers in my views !!!!!
Thank you for your help
Image of the issue:
The code:
func addPressed(_ sender: Any) {
let alert = UIAlertController (title: "Add an Action", message: nil, preferredStyle: UIAlertControllerStyle.alert)
let request = UIAlertAction (title: actionType.Request.Str(), style: UIAlertActionStyle.default, handler: {
action in
self.actionTypeTemp = actionType.Request.Str()
self.performSegue(withIdentifier: "segueActionDetailsVC", sender: nil)
} )
let take = UIAlertAction (title: actionType.Take.Str(), style: UIAlertActionStyle.default, handler: {
action in
self.actionTypeTemp = actionType.Take.Str()
self.performSegue(withIdentifier: "segueActionDetailsVC", sender: nil)
} )
let done = UIAlertAction (title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil )
alert.addAction(request)
alert.addAction(take)
alert.addAction(done)
self.present (alert, animated: true, completion: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueActionDetailsVC" {
if let destination = segue.destination as? ActionDetailsVC {
if let action = sender as? PActions {
destination.actionToEdit = action
}
else {
if actionTypeTemp == actionType.Request.Str() {
destination.relatedContact = self.contact
destination.title = actionType.Request.Str()
}
else {
destination.relatedContact = self.contact
destination.title = actionType.Take.Str()
}
}
}
}
}

Related

Unwind segue not dismissing view

The Issue:
Unwind segue not dismissing view, even though it works.
Scenario:
I have an "add product" screen, and when the product is added, I fire up unwind segue, which in turn shows a popup: "New Product added".
The Facts:
1) Product is successfully added to Database
2) the popup "Product added" is shown in the #IBAction of the unwind segue in target, and it appears
3) The popup "Product Added" is shown over the AddProductScreen view
Picture 1: Existence of Unwind segue
#IBAction located in target view:
#IBAction func unwindFromAddProductViewSuccess(segue: UIStoryboardSegue)
{
AlertManager.showTimedAlert(title: "Success", message: "New Product added", timeShowing: 1, callingUIViewController: self)
}
Function being called in AddProductView for registering the product:
private func registerProductAndPerformSegue(productToRegister prod: Product)
{
self.registerProduct(prodToRegister: prod)
Constants.logger.debug("New product registered")
self.performSegue(withIdentifier: "unwind_from_add_product_success", sender: self)
}
In the "Add Product View", after "Confirm" is clicked, an alert shows with "Are you sure?" and then you are prompt to click "Yes" or "No".
This is the code for the alert:
AlertManager.ShowAlert(controllerToShowAlert: self, alertTitle: LocalizationStrings.Products.ADD_PRODUCT_TITLE, alertText: LocalizationStrings.Products.ADD_PRODUCT_CONFIRMATION,
alertStyle: UIAlertControllerStyle.alert,
leftButtonAction: UIAlertAction(title: LocalizationStrings.YES_TEXT, style: .default, handler:
{
(action: UIAlertAction!) in
let user = Auth.auth().currentUser
if let user = user
{
let product : Product = Product(name: name!, price: Double(price!)!, currency: "USD", description: desc!,
location: "USA", ownerID: user.uid, ownerName: user.displayName!, uniqueID: "", mainImageURL: nil, category: category)
let storageRef = Storage.storage().reference().child(product.getUniqueID()).child("pic0.jpg")
if let mainChosenImage = self.selectedImageToUpload
{
Constants.logger.debug("Product picture chosen")
if let uploadData = UIImageJPEGRepresentation(mainChosenImage, 0.2)
{
storageRef.putData(uploadData, metadata: nil)
{
(StorageMetaData, error) in
if error != nil
{
Constants.logger.error("Add Product: Couldn't store image in database!")
return
}
self.mainImageURL = StorageMetaData?.downloadURL()?.absoluteString
if let urlString = self.mainImageURL
{
product.AddImageURLToProduct(URL: urlString)
self.registerProductAndPerformSegue(productToRegister: product)
}
}
}
else
{
Constants.logger.error("Couldn't convert uploaded image to UIImageJPEGRepresentation")
}
}
else
{
Constants.logger.debug("Product picture NOT chosen")
self.registerProductAndPerformSegue(productToRegister: product)
}
}
}),
rightButtonAction: UIAlertAction(title: LocalizationStrings.NO_TEXT, style: .cancel, handler: { (action: UIAlertAction!) in
print("Handle Cancel Logic here")
}))
What am I doing wrong here?
Without more details about your Storyboard setup and your code, it's hard to tell what the exact problem is.
But I'll assume that you have two view controllers and I will try to guide you through the steps to do an unwindSegue and you can check if you maybe have missed something.
Assuming that you have two view controllers: FirstViewController andAddProductViewController, andAddProductViewController` is presented using a push segue.
Inside FirstViewController you have:
#IBAction func unwindFromAddProductViewSuccess(segue: UIStoryboardSegue) {
AlertManager.showTimedAlert(title: "Success", message: "New Product added", timeShowing: 1, callingUIViewController: self)
}
Then, You connect your "save" or "add" button from AddProductViewController to unwindFromAddProductViewSuccess: action method.
You can also connect your "save" or "add" button to SecondViewController to debug that the segue is being called:
class SecondViewController: UIViewController {
#IBOutlet weak var saveButton: UIBarButtonItem!
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let uiBarButtonItem = sender as? UIBarButtonItem else {
print("There is no UIBarButtonItem sender")
return
}
if saveButton == uiBarButtonItem {
print("save button tapped")
}
print("A segue with an identifier \(segue.identifier ?? "") was called.")
}
}
Assuming that you don' have a bar button item and want to call unwidnSegue manually, maybe after the user taps on an UIAlertController action, then you can do something like this:
#IBAction func orderButtonTapped(_ sender: UIButton) {
let alert = UIAlertController(title: "Order Placed!", message: "Thank you for your order.\nWe'll ship it to you soon!", preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {
(_)in
self.performSegue(withIdentifier: "unwindToMenu", sender: self)
})
alert.addAction(OKAction)
self.present(alert, animated: true, completion: nil)
}
There are also some cases when the UIViewController that you are trying to unwind from is at the bottom of the UINavigationController stack, in this case, calling unwindSegue will not do anything. There was a similar question related to that before, here.
I hope that this info can help you figure out the problem.

How to pop view controller and then show UIAlertController

Say there is an error in ViewController2, and I want to go back to ViewController1, the previous view controller and then display an alert. Right now if I put this in ViewController2
#IBAction func testing(_ sender: Any) {
navigationController?.popViewController(animated: true)
Alert.alert(userTitle: "Error", userMessage: " ", userOptions: " ", in: LandingViewController() as UIViewController)
}
using this as the Alert class
public class Alert {
class func alert(userTitle: String?, userMessage: String, userOptions: String, in vc: UIViewController) {
DispatchQueue.main.async {
let alert = UIAlertController(title: userTitle, message: userMessage, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: userOptions, style: UIAlertActionStyle.default, handler: nil))
vc.present(alert, animated: true, completion: nil)
}
}
}
It will throw an error
So is there a way to do this?
The problem is that LandingViewController() as UIViewController is not rendered in UiWindow
try this
guard let nav = navigationController, let top = nav.topViewController else {
return
}
nav.popViewController(animated: true)
Alert.alert(userTitle: "Error", userMessage: " ", userOptions: " ", in: top)

How to navigate ViewController when clicking button in alertView

Given the code below, when I click the "Accept" button, it doesn't navigate to SecondViewController, it only shows a black screen. Any help appreciated.
let alertController = UIAlertController(title: tit!, message: "Book Now!", preferredStyle: .Alert)
let declineAction = UIAlertAction(title: "Decline", style: .Cancel, handler: nil)
alertController.addAction(declineAction)
let acceptAction = UIAlertAction(title: "Accept", style: .Default) { (_) -> Void in
let nv=SecondViewController()
self.presentViewController(nv, animated:true, completion:nil)
}
presentViewController(alertController, animated: true, completion: nil)
alertController.addAction(acceptAction)
To open any UIViewController just must instantiate it. And what you are doing is just creating the object of the class.
To do so:
let nv = self.storyboard!.instantiateViewControllerWithIdentifier("storyboardidentifier") as! SecondViewController
self.presentViewController(nv, animated:true, completion:nil)
This is open your UIViewController as wants!
Create a segue link from your current view controller to the SecondViewController and give it an identifier in the storyboard and you can use the following code
let alert = UIAlertController(title: "Options", message: "Book Now!", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Accept", style: UIAlertActionStyle.Default, handler: { (alertAction) -> Void in
self.performSegueWithIdentifier("FirstToSecond", sender: self)
}))
self.presentViewController(alert, animated: true, completion: nil)
Note: your first view controller will be the anchor.
If you have embedded your SecondViewController in a navigation controller and also want to pass the value, you can add the following
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "FirstToSecond" {
let nav = segue.destinationViewController as! UINavigationController
let svc = nav.viewControllers[0] as! SecondViewController
//svc.delegate = self //uncomment this if you need to set a delegate as well
svc.value = ""
}
}
This is using xCode 7 & Swift 2. Hope this helps :)

Creating a pop up dialog alert

I'm trying to create a popup uialert sort of to create a confirmation box. I have a button in ViewControllerTwo and when pressed navigates back to ViewControllerOne, however I want to create a popup message that asks to confirm (Yes or No) if I really want to navigate to ViewControllerOne. If yes it goes backs to ViewOne, if no it stays on the ViewTwo. How do I do this?
#IBAction func showAlertTapped(sender: AnyObject) {
//Create the AlertController
let myAlertController: UIAlertController = UIAlertController(title: "Hey..!", message: "Are You sure to Do some stuff??", preferredStyle: .Alert)
//Create and add the Cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
//Do some stuff
}
myAlertController.addAction(cancelAction)
//Create and an option action
let nextAction: UIAlertAction = UIAlertAction(title: "Next", style: .Default) { action -> Void in
let mainStoryboard = UIStoryboard(name: "Storyboard", bundle: NSBundle.mainBundle())
let vc : UIViewController = mainStoryboard.instantiateViewControllerWithIdentifier("vcMainLogin") as UIViewController
self.presentViewController(vc, animated: true, completion: nil)
}
myAlertController.addAction(nextAction)
//Present the AlertController
self.presentViewController(myAlertController, animated: true, completion: nil)
}

How do we create and dismiss an UIAlertController without user input? (Swift)

I've been looking up a lot of tutorials on UIAlertController. Thus far, the way I found was to activate a UIAlertController by linking it to a button or label and then call a IBAction.
I tried to replicate the code to automatically pop an alert when user enters the app (I wanted to ask the user if they want to go through the tutorial). However, I keep getting the error:
Warning: Attempt to present UIAlertController on MainViewController whose view is not in the window hierarchy!
Then I tried to add the UIAlertController to the MainViewController via addChildViewController and addSubview. However, I get the error:
Application tried to present modally an active controller
I figured that I cannot use the presentViewController function and commented it out.
The UIAlertController is displayed BUT when I tried to click on the cancel or the never button, this error occurs.
Trying to dismiss UIAlertController with unknown presenter.
I am really stumped. Can someone share what I am doing wrong? Thank you so much. Here is the code.
func displayTutorial() {
alertController = UIAlertController(title: NSLocalizedString("tutorialAlert", comment: ""), message: NSLocalizedString("tutorialMsg", comment: ""), preferredStyle: .ActionSheet)
self.addChildViewController(alertController)
self.view.addSubview(alertController.view)
alertController.didMoveToParentViewController(self)
alertController.view.frame.origin.x = self.view.frame.midX
alertController.view.frame.origin.y = self.view.frame.midY
//alertController.popoverPresentationController?.sourceView = self.view*/
let OkAction = UIAlertAction(title: NSLocalizedString("yesh", comment: ""), style: .Destructive) { (action) in
}
alertController.addAction(OkAction)
let cancelAction = UIAlertAction(title: NSLocalizedString("notNow", comment: ""), style: .Destructive) { (action) in
//println(action)
self.tutorial = 1
self.presentedViewController?.dismissViewControllerAnimated(true, completion: nil)
}
alertController.addAction(cancelAction)
let neverAction = UIAlertAction(title: NSLocalizedString("never", comment: ""), style: .Cancel) { (action) in
self.tutorial = 1
}
alertController.addAction(neverAction)
//self.presentViewController(alertController, animated: false) {}
}
I found the solution. Apparently, I cannot call the UIAlertController from the func viewDidLoad. I must call the function from viewDidAppear. So my code now is
override func viewDidAppear(animated: Bool) {
if tutorial == 0 {
displayTutorial(self.view)
}
}
func displayTutorial(sender:AnyObject) {
let alertController = UIAlertController(title: NSLocalizedString("tutorialAlert", comment: ""), message: NSLocalizedString("tutorialMsg", comment: ""), preferredStyle: .ActionSheet)
let OkAction = UIAlertAction(title: NSLocalizedString("yesh", comment: ""), style: .Destructive) { (action) in
}
alertController.addAction(OkAction)
let cancelAction = UIAlertAction(title: NSLocalizedString("notNow", comment: ""), style: .Default) { (action) in
//println(action)
self.tutorial = 1
self.presentedViewController?.dismissViewControllerAnimated(true, completion: nil)
}
alertController.addAction(cancelAction)
let neverAction = UIAlertAction(title: NSLocalizedString("never", comment: ""), style: .Cancel) { (action) in
self.tutorial = 1
}
alertController.addAction(neverAction)
self.presentViewController(alertController, animated: true, completion: nil)
if let pop = alertController.popoverPresentationController {
let v = sender as UIView
pop.sourceView = view
pop.sourceRect = v.bounds
}
}
Thanks to this posting: Warning: Attempt to present * on * whose view is not in the window hierarchy - swift
Below UIAlertController with extension would help you show alert with dynamic number of buttons with completion handler for selected index
extension UIViewController {
func displayAlertWith(message:String) {
displayAlertWith(message: message, buttons: ["Dismiss"]) { (index) in
}
}
func displayAlertWith(message:String, buttons:[String], completion:((_ index:Int) -> Void)!) -> Void {
displayAlertWithTitleFromVC(vc: self, title: Bundle.main.infoDictionary!["CFBundleDisplayName"] as! String, andMessage: message, buttons: buttons, completion: completion)
}
func displayAlertWithTitleFromVC(vc:UIViewController, title:String, andMessage message:String, buttons:[String], completion:((_ index:Int) -> Void)!) -> Void {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
for index in 0..<buttons.count {
let action = UIAlertAction(title: buttons[index], style: .default, handler: {
(alert: UIAlertAction!) in
if(completion != nil){
completion(index)
}
})
alertController.addAction(action)
}
DispatchQueue.main.async {
vc.present(alertController, animated: true, completion: nil)
}
}
}
If you need to auto dismiss the alert you can call dismiss on presented view controller after some delay.
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
vc.dismiss(animated: true, completion: nil)
}
Hope this might help you.