2

I am trying to present an alert to the user if they need to be logged in to access a feature but when I press the login button in the alert self.self.performSegueWithIdentifier("tryonToLogin", sender:self) does not appear to do anything.

LoginViewController

@IBAction func unwindToLogin(segue:UIStoryboardSegue){

}

UIAlert

 @IBAction func goToGallery(sender: UIButton){
    if(isLoggedIn){
        performSegueWithIdentifier("ShowGallery", sender: sender)
    }else{
        showMessage("You must login to view access this feature", sender:sender)
    }
}

func showMessage(popUpMessageText : String, sender:UIButton){
    let messageTitle = "Error"

    print("prepreform segue")
    performSegueWithIdentifier("unwindToLogin", sender:sender)

    let refreshAlert = UIAlertController(title: messageTitle, message: popUpMessageText, preferredStyle: UIAlertControllerStyle.Alert)

    refreshAlert.addAction( UIAlertAction(title: "Login", style: .Default, handler: { (action: UIAlertAction!) in
        print("Handle Login redirect logic here")
        self.performSegueWithIdentifier("unwindToLogin", sender: self)
        print("after Handle Login redirect logic here")
    }))

    refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: { (action: UIAlertAction!) in
        print("Handle Cancel logic here")
    }))


    presentViewController(refreshAlert, animated: true, completion: nil)

}

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    print("prepare for segue called")
    self.camera = nil
}

There are no errors. it is getting called as if ii put in a id that does not exist i get the no segue with specified identifier found error message. Does the UIAlert affect the calling of the performSegueWithIdentifier()

The following post uses this approach so I think it should work. what am i missing.

**IB Screenshots **

screenshot 1

enter image description here

Edit

I have tried moving self.performSegueWithIdentifier("unwindToLogin", sender:self) to view did appear and it still is snot working.

Lonergan6275
  • 1,938
  • 6
  • 32
  • 63

2 Answers2

0

The self inside the block is the UIAlertController, not the UIViewController. Create a weak reference to the UIViewController, and call the method on that object.

weak var weakSelf = self

let refreshAlert = UIAlertController(title: messageTitle, message: popUpMessageText, preferredStyle: UIAlertControllerStyle.Alert)

refreshAlert.addAction( UIAlertAction(title: "Login", style: .Default, handler: { (action: UIAlertAction!) in
    print("Handle Login redirect logic here")
    dispatch_async(dispatch_get_main_queue()) {
        weakSelf?.performSegueWithIdentifier("unwindToLogin", sender:self)
    }
}))
Dávid Kaszás
  • 1,877
  • 1
  • 16
  • 20
  • I was thinking something like this might be the issue however it still does not work. – Lonergan6275 Jun 01 '16 at 14:57
  • add a func tmp() { print("tmp") } method to your ViewController and call weakSelf?.tmp() to check if the tmp method is called. – Dávid Kaszás Jun 01 '16 at 14:59
  • okay, then the problem is with the performSeque. comment out the alert and call self.performSegueWithIdentifier("unwindToLogin", sender:self) to check if it is working. also check the seque's name in the IB. – Dávid Kaszás Jun 01 '16 at 15:04
  • shit I was wrong, the self inside the block is referring to the UIViewController, you don't need this weakSelf stuff at all. – Dávid Kaszás Jun 01 '16 at 15:07
  • i have added screenshots with the segue. i have also tried moving the `performSegueWithIdentifier("unwindToLogin", sender: self)` to viewDidLoad() as a test and it still does not work. im thinking i may be forgetting some small fundamental step somewhere – Lonergan6275 Jun 01 '16 at 15:23
0

Seeing that you have not provide sufficient informations, I present you this 'solution'.

  • Create an extension of UIViewController in order to implement facility function to implement an alertView-
  • I suppose that you put your code on "viewDidLoad": you have to move code on "viewDidAppear".
  • please check what operation you do in prepareForSegue

I think this will be sufficient.

extension UIViewController {

    /**
     Show alert view with OK/Cancel button.

     - parameter title:       alert title
     - parameter message:     alert message
     - parameter onOk:        callback on ok button tapped
     - parameter onCancel:    callback on cancel button tapped

     - returns: alert controller
     */
    func showAlert(title title: String, message: String, onOk: (() -> ())?, onCancel: (() -> ())? = nil) -> UIAlertController {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
        let okAction = UIAlertAction(title: "Ok", style: .Default) { (alert: UIAlertAction!) -> Void in
            onOk?()
        }
       let cancelAction = UIAlertAction(title: "Cancel"), style: .Default) { (alert: UIAlertAction!) -> Void in
            onCancel?()
        }
        alertController.addAction(okAction)
        alertController.addAction(cancelAction)
        self.presentViewController(alertController, animated: true, completion: nil)
        return alertController
    }

}

class LoginViewController: UIViewController {
      override func viewDidAppear(animated: Bool) {
         super.viewDidAppear(animated) 
         let title = "Login"
         let message = messageTitle
         self.showAlert(title: title, message: message, onOk: { () -> () in
            self.performSegueWithIdentifier("unwindToLogin", sender: self)
         })  
      }
}
Luca Davanzo
  • 21,000
  • 15
  • 120
  • 146
  • Im sorry but don't see how this applies to my question. and you provided no explination of where you are coming from with this approach – Lonergan6275 Jun 01 '16 at 15:26
  • edited... I really think this code will help you, there are no reasons why it should not work. – Luca Davanzo Jun 01 '16 at 15:34
  • I think this might be over kill as my problem seems to be `self.performSegueWithIdentifier("unwindToLogin", sender: self)` is not working where ever it is called. the only way i know it is called is be passing an id that is not valid – Lonergan6275 Jun 01 '16 at 15:42