Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ioannis Pavlidis Dinesh Majeti Ashik Khatri

Similar presentations


Presentation on theme: "Ioannis Pavlidis Dinesh Majeti Ashik Khatri"— Presentation transcript:

1 Ioannis Pavlidis Dinesh Majeti Ashik Khatri
Lecture 3 Ioannis Pavlidis Dinesh Majeti Ashik Khatri

2 Overview Protocols Delegates UIStackView

3 Protocols Specifies a list of methods like a “contract” or “interface”
A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality Any type that satisfies the requirements of a protocol is said to conform to that protocol Protocols can be made optional and optional keywords

4 Protocols Defined similar to classes, structures, and enumerations
Property requirements are declared as variable properties protocol SomeProtocol { func doSomething() var mustBeSettable: Int { get set } var doesNotNeedToBeSettable: Int { get } } @objc protocol SomeProtocol { optional func doSomething()

5 Implementing a protocol
protocol RandomNumberGenerator { func random() -> Double } class SomeClass: RandomNumberGenerator { var lastRandom = 42.0 let m = let a = let c = func random() -> Double { lastRandom = ((lastRandom * a + c) % m) return lastRandom / m }

6 Delegation Enables a class or structure to hand off (or delegate) some of its responsibilities to an instance of another type. Can be used to respond to a particular action Retrieve data from an external source without needing to know the underlying type of that source.

7 Example Start with a simple protocol protocol UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool }

8 Protocol as a Type class UITextField {
var delegate: UITextFieldDelegate? var text: String? //… some other stuff }

9 Delegating responsibility
class Controller: UITextFieldDelegate { var textField = UITextField() textField.delegate = self func textFieldShouldReturn() -> Bool { // do something when text field returns. }

10 Delegates everywhere UITableView
Example: When a table row is tapped, when a table view cell is displayed, etc. UITextField Example: Notifying when the user stops typing, hits the return key, etc Classes like UITableView, UITextField, do not provide implementation details for certain events. These are delegated off to any class that conforms to the protocols

11 UIStackView Provides a streamlined interface for laying out a collection of views in either a column or a row Creating user interfaces that can dynamically adapt to the device’s orientation, screen size, and any changes in the available space Uses AutoLayout to position and size its arranged views Stack views can be nested

12 UIStackView Manages layout of all the views in its arrangedSubviews property Arranged along the stack view axis, based on their order in the arrangedSubviews array The exact layout varies depending on the stack view’s properties like axis, distribution, alignment, spacing

13 References Protocols and Delegates UIStackView Tutorial
More on UIStackView


Download ppt "Ioannis Pavlidis Dinesh Majeti Ashik Khatri"

Similar presentations


Ads by Google