Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Christian A. Castro
Christian A. Castro
30,501 Points

Any idea why the Alert Box is not displaying? Interactive App! Any feedback highly appreciate it!!! Thanks in advance!!

The alert box is not currently displaying in the ap... I placed a breakpoint on the "catch AdventureError.nameNotProvided" statement and I noticed is not getting triggered... any idea?

`` class ViewController: UIViewController { //..Reference of the TextField.., we can't start the storty without a name.. @IBOutlet weak var nameTextField: UITextField!

override func viewDidLoad()
{
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning()
{
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

//..Preparing the segue... when the segue is about to fire..
//..the segue object and the sender two arguments.. 
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
    if segue.identifier == "startAdventure"
    {
        //..Checking if a name has been provided..
        do {
          if let name = nameTextField.text {
            if name == " "
            {
                //..Thrworing error..
                throw AdventureError.nameNotProvided
             }
            else
            {
                guard let pageController = segue.destination as? PageController
                else
                {
                    return;
                }
                pageController.page = Adventure.story(withName: name)
            }
        }
    }
    //..Handling the error..
    catch AdventureError.nameNotProvided
    {
        //.Displayiing the alet message, you haven't provided a name..
        let alertController = UIAlertController(title: "Name haven't been provided", message: "Provided a name", preferredStyle: .alert )

        let action = UIAlertAction (title: "OK", style: .default, handler: nil )
        //..Adding action to the alertController variable..
        alertController.addAction(action)

        //.Presenting that to the ViewController..
       present(alertController, animated: true, completion: nil)    
    }
    catch let error
    {
            //..Throwing error on purpose.., 
            fatalError("\(error.localizedDescription)")
    }
  }

``