Lesson 2 Code Reference Sheet

This outline is intended as a quick way to reference code snippets. It includes any code from this lesson that you were expected to write yourself and lists files available for download.

How does a view get presented modally?

// Present the image picker
let controller = UIImagePickerController()
self.presentViewController(controller, animated: true, completion:nil)

Launch Image Picker and Activity View

// Present the image picker
@IBAction func experiment() {
    let controller = UIImagePickerController()        
    self.presentViewController(controller, animated: true, completion: nil)
}

Launch Activity View and Alert View

In the ViewController.swift file of your experiment app:
Launch the Activity View Controller:

// Present the Activity View Controller        
let image = UIImage()
let controller = UIActivityViewController(activityItems: [image], applicationActivities: nil)
self.presentViewController(controller, animated: true, completion: nil)

Launch the Alert View Controller:

@IBAction func experiment() {
    let controller = UIAlertController()
    controller.title = "Test alert"
    controller.message = "This is a test"

    // Dismiss the view controller after the user taps “ok”    
    let okAction = UIAlertAction (title:"ok", style: UIAlertActionStyle.Default) {
        action in self.dismissViewControllerAnimated(true, completion: nil)
    }
    controller.addAction(okAction) 
    self.presentViewController(controller, animated: true, completion:nil)
}

Summarize code for the three presentation experiments

Image Picker Controller

@IBAction func experiment() {
    let controller = UIImagePickerController()        
    self.presentViewController(controller, animated: true, completion: nil)
}

Activity View Controller

   
@IBAction func experiment() {
    let image = UIImage()
    let controller = UIActivityViewController(activityItems: [image], applicationActivities: nil) 
    self.presentViewController(controller, animated: true, completion: nil)
}

Alert View Controller

@IBAction func experiment() {
    let controller = UIAlertController()
    controller.title = "Test alert"
    controller.message = "This is a test"

    // Dismiss the view controller after the user taps “ok”    
    let okAction = UIAlertAction (title:"ok", style: UIAlertActionStyle.Default) {
        action in self.dismissViewControllerAnimated(true, completion: nil)
    }
    controller.addAction(okAction) 
    self.presentViewController(controller, animated: true, completion:nil)
}

Dice example, take control

Download Dice_incomplete

Presenting View Controllers: Code Only

The rollTheDice action method

@IBAction func rollTheDice() {
     // Get the DiceViewController
    var controller: DiceViewController
    controller = self.storyboard.instantiateViewControllerWithIdentifier("DiceViewController") as DiceViewController

    // Set the two values to random numbers from 1 to 6   
    controller.firstValue = Int(1 + arc4random() % 6)
    controller.secondValue = Int(1 + arc4random() % 6)

    // Present the view Controller
    self.presentViewController(controller, animated: true, completion: nil)
}

Demonstrate Code & Segue

func rollTheDice() {                    
    performSegueWithIdentifier("rollDice", sender: self)
}

Review of prepareForSegue

Download PrepareForSegueReview

func prepareForSegue(_ segue: UIStoryboardSegue, sender sender: AnyObject?) {
        let controller = segue.destinationViewController as DiceViewController

        controller.firstValue = self.randomDiceValue()
        controller.secondValue = self.randomDiceValue()
    }

Steps to Build Roshambo

When you are finished download Roshambo_exampleSolution and for a fancier solution download Roshambo_separateModel