IOS with Swift Hello world app. Step 1 launch XCode Version 6 or more for Swift Create New Xcode...

52
iOS with Swift Hello world app

Transcript of IOS with Swift Hello world app. Step 1 launch XCode Version 6 or more for Swift Create New Xcode...

Page 1: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

iOS with SwiftHello world app

Page 2: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Step 1 launch XCode Version 6 or more for Swift Create New Xcode project

Page 3: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Step 2: Choose type of project Master-Detail Applicaton: This good for

Ipad (has list on left and details on larger right area)

Page 4: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Step 2: Choose type of project Page-Based Applicaton: Scrolling

application (like iPhone home screen)

Page 5: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Step 2: Choose type of project Single View Application: simple with

only 1 view

Page 6: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Step 2: Choose type of project Tabbed Application: tabs on bottom

application (like app store application)

Page 7: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Step 2: Choose type of project Empty Application: no views specified

Page 8: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Step 2: Choose type of project Game: no views specified

Page 9: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Step 3: name project & select Swift name of application, organization, and

select Swift language. Also, specify iPhone or iPad or.. Next ---and save it somewhere on

machine

Page 10: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Step 4: setup project properties

We are not going to change anything from defaults except to specify it in “portrait” orientation

• Identity: (version, etc) Deployment : (target SDK, device, main interface, orientation)

• Icons/Launch Images• Embedded Binaries• Linked

Frameworks/Libraries

Page 11: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Lets look at File structure AppDelegate.swift

Communicates with phones iOS system, tells about this app

Replaces AppDelegate.m and .h from Objective-C

ViewController.swift Has lifecycle methods of app and setups up main

window for app. Often don’t edit

Main.storyboard Used to setup interface(s) with a visual editor

Images.xcassets Supporting Files HelloWorldSwiftTests = contains any testing code Products = contains

Page 12: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

AppDelegate.swiftAuto generated with project

Creating a window

Various app lifecyclemethods

Not going to RIGHTNow alter anything here

Page 13: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

ViewController Has method viewDidLoad()

that we will alter to setup –like loading from nib a view

Page 14: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Main.storyboard Has Visual Editing interface

that lets us specify multiple Viewsand their interfaces

Page 15: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Main.storyboard interface Can visually edit

Page 16: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Step 6: add “hello world” to single view interface we have in our Main.storyboard

Step 6.1: select View controller

Page 17: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Step 6.2: Step 6.1: select View controller

Page 18: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Step 6.3: bring up Objects Inspector Use Objects Inspector to select widgets you

want to drag and drop to your interface

Page 19: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Step 6.4: in inspector window Set Simulator Metrics Here choose the smaller iPhone 4 option in

portrait view

Page 20: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Step 6.5: find Label in Objects Inspector drag and drop

Page 21: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Step 6.6: change properties of Label in inspector Change text, size, center it

Page 22: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Step 6.6: Add a Text field Make it width bigger, setup properties:

set text that initially appears (placeholder)

Page 23: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Step 7: Add some Event handling code so your gui works Here when user types in something in

the Text Field you will read it in and change the Label to say Hello “Name Entered”

Page 24: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Step 7.1: bring up assistant editor Lets you see storyboard AND code

Page 25: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Step 7.1: assistant editor—what it looks like

Code –here ViewControllerStoryboard

Page 26: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Step 7.2: Create IBoutlets for all GUI widgets you want “handles to in your code

1. Select Gui Widget in Storyboard2. Right Click on it and drag to

ViewController code below the class statement

Page 27: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Step 7.2: Create IBoutlets for all GUI widgets you want “handles to in your code

3. Now release and fill in pop-up --- give it a name (here “nameLabel”) and hit CONNECT

4. Now you will have the following code

IBOutlet is a kind of“connection” like anelectrical outlet betweencode and the GUI

NOTE: the nil here does not mean a nil object but, ratherwe are not changing the propertiesor text of the Label in the GUI

Page 28: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Step 7.3: Create IBActions for all GUI widgets you want handle events fromHere we want to handle the even when user types (something first) and hits return in the Text Field GUI.1. Select Gui Widget in Storyboard2. Right Click on it and drag to ViewController code

before end of class

Page 29: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Step 7.3: Create IBActions for all GUI widgets you want handle events from

3. Now release and fill in pop-up --- Connection = action Event = “Did End On Exit” Type = “UIText Field” Argument= sender Name=(here “helloWorldAction”)

Different Types,End on Exit will be createdwhen user hits return in text box

Creates dummy functionthat YOU must edit to createcode to handle this event

Page 30: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Step 7.3: create even handling code

Change the name of the parameter form sender to nameTextField (better to be specific)

Grab the text typed in the Text Field Create string to alter the label.

Remember “nameLabel” is the variable (IBOutlet) pointing toLabel

SWIFT• Set text the setter is

just .texton nameLabel

• Print out variable in string \(varname)

• NO NEED for SEMI-COLON at end of lines in swift

Page 31: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Step 7.4: tip: getting rid of keyboard that the TextField popped up

Then inside the event handler say that the UITextField should give up its focus

@IBAction func helloWorldAction(nameTextField : UITextField) { nameLabel.text = “Hi \(nameTextField.text)” nameTextField.resignFirstResponder()}

This says that the UIField nameTextField is giving up its FirstResponse status ---which means it is no longer in focus, And the associated keyboardwill dissapear

Page 32: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Step 8: run the program To run your program hit the “play/run”

button in upper left of xcode

You can change device typehere currently set at iPhone 4 towhatever you want, like iPhone 6

Page 33: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

A note about Storyboard Can have multiple interfaces and connections between them. a single file describes all of this These between interfaces transitions are called “segues”

you create them by connecting your view controllers right in the storyboard.

Page 34: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Special Note about First Responder won’t be using the First Responder very

much. a proxy object that refers to whatever

object has first responder status at any given time.

Page 35: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

How does everything launch??? AppDelegate class

@UIApplicationMain =says this classis launch/entry point

application() function will be calledNO CODE??? - see next slide

Page 36: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

It looks to Info.plist

Contains information (like Android Manifest) about the app

HERE specifies the storyboard to launch

Located in Supporting Files groupIn android like loading up the main layout for an Activity

Page 37: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

How to change the name of the Storyboard file Go to project settings

Page 38: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Storyboard with multiple viewsGetting started ….

Page 39: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Bring up Storyboard Zoom out so have room in interface Add “View Controller” to Storyboard

Page 40: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Results..of adding 2nd View Controller

Page 41: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Change background colors &add 3rd View Controller &add SEGUES Add some buttons that we will have Actions for

to transition from one View to another Segue = Cntrl-Click on button in #1 interface

and drag to #2 interface Type = show, present modally or …..

Page 42: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Result ---see relationship between 2 views

Lets Run it

Page 43: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Do similar with 2nd View’s button to segue to 3rd View Run it

Page 44: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Suppose we want to add NAVIGATION Controller to 1st View Select 1st view in storyboard Menu: Editor->Embed In ->Navigation

Controller

Inserts a “Navigation Controller for entire app

Page 45: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Lets Run Right now no navigation controls added

yet but, see bar area at the top. Will be there for every View in this

Storyboard NOTICE: it adds Back link in 2nd View

automatically

Page 47: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Example 2: Storyboard w/ multiple Views—started –see http://www.raywenderlich.com/81879/storyboards-tutorial-swift-part-1

Page 48: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Example multiple viewswith Storyboard Go to  Main.storyboard and delete the

view controller you worked with earlier. This can be done by clicking on View Controller in the Document Outline and pressing the Delete key.

Drag a Tab Bar Controller from the Object Library into the canvas.

the Tab Bar Controller comes with two view controllers

ZOOM tip: zoom in and out by double-clicking the canvas, or set zoom scale by ctrl-clicking the canvas and selecting the zoom level.

Page 50: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Example multiple viewswith Storyboard

 Tab Bar Controller with two view controllers

Page 51: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Multiple view Storyboard example—notice the relationship Between Tab Bar Controller and one of it

view controllers

Page 52: IOS with Swift Hello world app. Step 1 launch XCode  Version 6 or more for Swift  Create New Xcode project.

Finish the example at http://www.raywenderlich.com/81879/storyboards-tutorial-swift-part-1

also see http://www.swiftvideotutorials.com/using-storyboards-for-osx