Skip to content

Latest commit

 

History

History
75 lines (60 loc) · 5.47 KB

2015_08_09.md

File metadata and controls

75 lines (60 loc) · 5.47 KB

Objectives

  • Make an object conform to a protocol
  • What are the pros and cons of open source?

Think Piece

http://www.ted.com/talks/angela_lee_duckworth_the_key_to_success_grit?language=en

Protocols

http://rypress.com/tutorials/objective-c/protocols
https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/Protocol.html#//apple_ref/doc/uid/TP40008195-CH45-SW1

Delegation

https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html image

Exercises

UIAlertView
http://code.tutsplus.com/tutorials/ios-sdk-working-with-uialertview-and-uialertviewdelegate--mobile-3159

  • In viewDidLoad, create and show a UIAlertView.
  • Make sure your view controller conforms to the UIAlertViewDelegate protocol
  • Implement some UIAlertViewDelegate methods with NSLogs

UIAlertViewController
As of iOS 8, Apple introduced a new way to handle alerts, UIAlertController.

  • Create another alert using UIAlertController Notice that there is no delegate for UIAlertController. What are some pros a cons to the new approach?

UITextField

  • Add a UITextField to your storyboard.
  • Drag from your UITextField int your code, creating a property in your view controller.
  • In viewDidLoad, set the delegate property of the UITextField to self (the view controller)
  • Make sure that your view controller class *conforms to the UITextField (<UITextFieldDelegate)
  • Which method would you use to disable editing of the UITextField?
  • Add a UISwitch. Only allow editing when the UISwitch is set to on.
  • Implement the textFieldDidEndEditing method. What do you need to do to trigger this method (hint: first responder)?

UIPickerView https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIPickerViewDelegate_Protocol/index.html
https://developer.apple.com/library/ios/documentation/iPhone/Reference/UIPickerViewDataSource_Protocol/index.html#//apple_ref/occ/intf/UIPickerViewDataSource

  • Add a UIPickerView to your storyboard.
  • Drag from your UIPickerView into your code, creating a property in your view controller.
  • In viewDidLoad, set the delegate property of the UIPickerView to be self (the view controller)
  • In viewDidLoad, set the dataSource property to be self (the view controller)
  • Make sure that your view controller class *conforms to the UIPickerViewDelegate protocol and the UIPickerViewDataSource protocol. (<UIPickerViewDelegate, UIPickerViewDataSource>)
  • Implement the required methods to render and interact with the UIPickerView
  • Fun ensues

Roll your own

  • Create a UINavigationController with a UITableViewController as its root view controller.
  • Create a new class called CQColorTableViewController (subclass of UITableViewController) and change the UITableViewController in the storyboard to CQColorTableViewController.
  • In CQColorTableViewController.m add @property (nonatomic) NSMutableArray *colors;
  • Implement the UITableViewDataSource methods so that there is 1 section and there is a row for every item in your array.
  • Head back into the storyboard.
  • In the UINavigationBar of the UINavigationController, add a + to the top right. image
  • Add a modal segue from the + button to a new UIViewController
  • Make a new class called CQColorPickerViewController and change your new UIViewController to a CQColorPickerViewController
  • In your CQColorPickerViewController add 6 buttons. Make the button titles the names of some colors. List of colors

image

  • Create an action for when a user taps on one of the buttons (drag from storyboard to view controller).
  • In your new method, add code to dismiss the modal view controller
  • Create a new Objective-C file. Make sure you choose protocol from the drop down. Name it CQColorPickerViewControllerDelegate

new file protocol

  • In CQColorPickerViewControllerDelegate.h, in between @protocol and @end, add one method:
    - (void)colorPickerViewController:(CQColorPickerViewController *)viewController didPickColor:(UIColor *)color;
  • Head over to CQColorPickerViewController.h. Create a new property: @property (nonatomic, weak) id<CQColorPickerViewControllerDelegate>delegate;. This is creating a property of type id called delegate. The stuff in the <> is saying that this object must conform to the CQColorPickerViewViewControllerDelegate.
  • In CQColorTableViewController.m, import CQColorPickerViewControllerDelegate. Make CQColorTableViewController conform to the CQColorPickerViewControllerDelegate protocol and implement the one method that we created, colorPickerViewController:didPickColor: