intro to ios development • made by many

18

Upload: kenatmxm

Post on 12-Jul-2015

390 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Intro to iOS Development • Made by Many
Page 2: Intro to iOS Development • Made by Many

MVC Architecture

Objective-C Syntax

Classes

Instances

Methods

Properties

Setters + Getters

Header + Implementation

Delegates + Protocols

Xcode IDE

Key Classes + Terms In Objective-C

In This Deck

This deck is shared with you under a Creative Commons license.

Page 3: Intro to iOS Development • Made by Many

Model

Contains all application logic and data

View

Your application’s graphical user interface

The MVC architecture is useful because it lets you abstract apart and organize your app into functional, replaceable modules.

View Controller

Coordinates between model and viewNSNotification

Cloud Services User Input

MVC Architecture(Model + View + Controller)

Page 4: Intro to iOS Development • Made by Many

Model View Result

Tic Tac Toe Visual

Tic Tac Toe Textual

+ =

X played: (2, 3)

O played: (3, 1)

X played: (1, 3)

SMS TextualAdam said: nm, u?

HJ said: sup

Chris said: CoD?

same model • different view

different model • same view

MVC Architecture(Model + View + Controller)

Page 5: Intro to iOS Development • Made by Many

Objective-CSyntax

Objective-C is a verbose language.

Method names and variable names tend to be very long.This is supposed to make it easier to understand your code and how to use methods.

[[Vehicle alloc] initWithMake:@“Toyota” model:@“Prius” color:[UIColor blackColor]];

Page 6: Intro to iOS Development • Made by Many

Objective-CSyntax

Objective-C is an object-oriented language.Methods are really “messages” send to objects or classes telling them to do things.

For example:[myCar setSpeed:50.0];

is really saying:“Tell myCar to set its speed to 50.0 mph.”

50.0 mph, please!

Page 7: Intro to iOS Development • Made by Many

Objective-CSyntax

Xcode will attempt to autocomplete whatever you’re typing.

This not only helps you save time (you can hit Tab to autocomplete) but also helps you discover relevant methods and properties.

Page 8: Intro to iOS Development • Made by Many

Objective-CClasses

Think of a class as a category, e.g., Vehicle.A class has no data but can perform some basic methods.

A class also specifies what properties instances of that class should have.It’s kind of like a form that hasn’t been filled out.

E.g., Vehicle might have properties for make, model, and speed.

Vehicle myCar yourCar

> make @“Toyota” @“Lamborghini”

> model @“Prius” @“Aventador”

> speed 50.0 95.0

Page 9: Intro to iOS Development • Made by Many

Objective-CClasses

All classes in Objective-C inherit some methods and properties from a superclass.As a result, each class is a subclass of another class.

E.g., Vehicle might be a subclass of NSObject and might have its own subclasses Car, Truck, and Motorcycle that have all the properties and methods of Vehicles but also add their own properties and

methods.

Vehicle

Truck MotorcycleCar

Page 10: Intro to iOS Development • Made by Many

An instance is a specific instance of a class.E.g., myCar might be an instance of Car.

An instance has data and may have some methods that you can perform on that data.

E.g., For myCar, you would have the following values for your properties:myCar.make = @“Toyota”;myCar.model = @“Prius”;

myCar.speed = 55.0;myCar.gas = 10.0;

myCar.owner = @“Ken M. Haggerty”;

Instances are often created using alloc and init, e.g.:[[Car alloc] init];

or using a “convenience” method, e.g.:[Car carWithOwner:@“Ken M. Haggerty”];

Objective-CInstances

Page 11: Intro to iOS Development • Made by Many

A class method is a method that is performed on a class rather than a specific instance of a class.Class methods are often useful for instantiation and general methods not specific to instances.

E.g., [NSArray arrayWithObject:firstObject] creates a new array with one object in it,or, [TicTacToe howManyInARowToWin] might return 3.

An instance method is a method that is performed on an instance of a class.Instance methods are useful for changing or manipulating instances of your class.

E.g., If myString is set to @“filename”,[myString stringbyAppendingString:@“.png”] would return the string @“filename.png”.

Objective-CMethods

Page 12: Intro to iOS Development • Made by Many

Properties are how data are stored in instances of your class.They are like fields on a form.

Each property defines what kind of data it should hold, but only instances of your class actually hold that data.

E.g., Car might have the property@property (nonatomic) BOOL isNewCar;

while myCar, an instance of Car, might have isNewCar set to NO.

Objective-CProperties

Page 13: Intro to iOS Development • Made by Many

When you create a property, Objective-C will automatically create helper methods for accessing and setting values for your property.

I.e., When you create:@property (nonatomic, strong) NSString *owner;

Objective-C will create the following two instance methods for you:- (void)setOwner:(NSString *)owner;

- (NSString *)owner;

Both of these methods are accessible to you as soon as you define your property.However, you can always write your own custom implementation for your helper and/or getter.

Objective-CSetters + Getters

Page 14: Intro to iOS Development • Made by Many

Setters are only called when that property is being set explicitly, e.g.:[myCar setOwner:@“Ken M. Haggerty”];myCar.owner = @“Ken M. Haggerty”;

Getters can be called in either of the following ways:[usa name];usa.name;

The second syntax in each of the above examples is called dot notation and is usually only used for properties in Objective-C.

Objective-CSetters + Getters

Page 15: Intro to iOS Development • Made by Many

“Burger, please!”(returns burger)

<YourClass>.h

Dictates public interface for this class; i.e., how other classes can and should interact with this class.

● Imports (#import)● Protocols (@protocol)● Public API (@interface)

<YourClass>.m

Dictates private interface for this class + contains all method implementations for this class.

● Imports (#import)● Private API (@interface)● Implementation (@implementation)

In Objective-C, the header (.h) file is “public” while the implementation file (.m) is “private.”

The header file should be organized, clear, and state what the class does and how to use it.

Objective-CHeader + Implementation Files

Page 16: Intro to iOS Development • Made by Many

In well-written Objective-C, your model and your view are completely agnostic of each other.I.e., Your model should not be in any way dependent on how it is ultimately presented to the user, and your

view should not care what kind of data it is representing.

Instead, your model and view should communicate using the following relationships:● Your view controller can change your model.

● Your model can change itself.● Your model can send out notifications when it is changed.

● Your view controller can listen for notifications and act accordingly.● Your view controller can change your view.

To do this, we often used delegates to delegate methods and properties from view controllers to models.E.g., UITableViewController has two delegates: delegate and dataSource.

When assigning delegates, we can ensure that our delegates implement the necessary properties and methods by specifying protocols that they should follow.

We can dictate what protocols a property should follow when defining them. E.g.:@property (nonatomic, weak) id <MyProtocol> delegate;

Objective-CDelegates + Protocols

Page 17: Intro to iOS Development • Made by Many

Xcode IDE(Integrated Development Environment)

There’s a lot going on in Xcode, so we’ll explore by clicking around.If you have trouble using Xcode or have any questions, don’t hesitate! Ken will be happy to help.

Demo

Page 18: Intro to iOS Development • Made by Many

NSObject or id: Top-level object in Objective-C. Almost all objects in Objective-C inherit from NSObject.

UIViewController: A basic view controller in Objective-C. All view controllers inherit from UIViewController.

UIView: A basic view in Objective-C. Views are always rectangles. All views inherit from UIView. Views can be placed as subviews within another view.

UIButton: A basic button in Objective-C. Buttons can respond to touch events.

UIImage: An image in Objective-C.

UIImageView: A subclass of UIView designed for displaying an image.

UIScrollView: A subclass of UIView designed for scrolling content. Scrollviews are tricky but incredibly useful.

IBOutlet / IBAction: Designates that a property / method should be connected to a view via Interface Builder.

nonatomic / atomic: You will always use nonatomic. This has to do with thread safety regarding setters and getters for properties.

strong / weak: Determines whether a property’s value should be held even if no other object is pointing to that value. Strong = yes, weak = no.

self: Used within an instance method to refer to the instance the method was sent to.

Objective-CKey Classes + Terms

Apple has very thorough documentation available from within Xcode at Help > Documentation and API Referenceand online at https://developer.apple.com/library/ios/navigation/

Interface Builder / Storyboard: Visual interface within Xcode IDE for laying out and creating your app’s GUI.

UITableViewController: A subclass of UIScrollView designed for presenting scrollable tables.

UITableView: A subclass of UIScrollView designed for presenting scrollable tables.

UITableViewCell: A subclass of UIView designed for presenting content in tables. UITableViewCells are reused as the table view is scrolled, increasing responsivity and smoothness.

NSArray: A subclass of NSObject that contains an uneditable list of NSObjects.

NSMutableArray: A subclass of NSArray where its list of NSObjects are editable.

NSString: A subclass of NSObject for text.

BOOL: A binary type that’s actually just plain C. Can have value YES or NO.

int: A C class for storing integer values.

float: A C class for storing floats (i.s., decimal values).

#import: Add a library, framework, or another class to the current class.