Katherine Jenkins Katherine Jenkins - 6 months ago 82
iOS Question

Using UIAlertController Action To Programatically Link To Embedded UIViewController

The user is currently on a ViewController that was presented

Modally
.

I am trying to hook up a UIAlertViewController's
OK
Button Action to programatically link to a
UIViewController
embedded in a
UINavigationController
.

Like so:

enter image description here
This is a snippet of my relevant code:

func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {

....// some code

case .Restored:

....// some code
let alert = UIAlertController(title: "Thank You!", message: "You now have FULL ad-free Access", preferredStyle: UIAlertControllerStyle.Alert)
let OKAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (action:UIAlertAction) in
// Goto Main Page:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("MainMainViewController");
self.navigationController?.presentViewController(vc, animated: true, completion: nil)
}
alert.addAction(OKAction)
self.presentViewController(alert, animated: true, completion: nil)

break;

default:
break;
}


Essentially, when the user restores a purchase (or makes a successful), I would like them to click
OK
to send them to another ViewController -
MainMainViewcontroller
.

But when I click the
OK
button, nothing happens.

Where am I going wrong?

Thanks in advance kind Sirs/Mesdames ;)

Answer

Write this way.

func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {

....// some code

case .Restored:

....// some code
let alert = UIAlertController(title: "Thank You!", message: "You now have FULL ad-free Access", preferredStyle: UIAlertControllerStyle.Alert)
let OKAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (action:UIAlertAction) in
// Goto Main Page:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let navVC = storyboard.instantiateViewControllerWithIdentifier("Navigation");
self.presentViewController(navVC, animated: true, completion: nil)
}
alert.addAction(OKAction)
self.presentViewController(alert, animated: true, completion: nil)

break;

default:
break;
}