IOS Swift language 1st Tutorial

85
IPhone Application Development Week 1 Muhammad Asif Subhani Assistant Professor

Transcript of IOS Swift language 1st Tutorial

Page 1: IOS Swift language 1st Tutorial

IPhone Application DevelopmentWeek 1

Muhammad Asif SubhaniAssistant Professor

Page 2: IOS Swift language 1st Tutorial

IPhone Application Development

• What is this class all about? Why am I here?

• PrerequisitesYou must be a strong object-oriented programmer.

• iOS Overview What’s in iOS?

Page 3: IOS Swift language 1st Tutorial

What will I learn in this course? • How to build cool apps

Easy to build even very complex applications.Result lives in your pocket or backpack!Very easy to distribute your application through the AppStore. Vibrant development community.

• Real-life Object-Oriented ProgrammingThe heart of Cocoa Touch is 100% object-oriented.

• Application of MVC design model.• Many computer science concepts applied in a commercial

development platform: Databases, Graphics, Multimedia, Multithreading, Animation, Networking, and much, much more!

Page 4: IOS Swift language 1st Tutorial

Prerequisites• Prior Coursework

Object-Oriented Programming experience mandatory. Structured Programming and Data Structure.

• You should know well the meaning of these terms ... Class (description/template for an object)Instance (manifestation of a class)Message (sent to object to make it act) Method (code invoked by a Message)Instance Variable (object-specific storage)Superclass/Subclass (Inheritance)If you are not very comfortable with all of these, this is probably not the class for you!

• Programming Experience This is an upper-level CS course. If you have never written a program where you had to design and implement more than a handful of classes, this will be a big step up in difficulty for you.

Page 5: IOS Swift language 1st Tutorial

What’s in iOS?

Page 6: IOS Swift language 1st Tutorial

What’s in iOS?

Page 7: IOS Swift language 1st Tutorial

What’s in iOS?

Page 8: IOS Swift language 1st Tutorial

What’s in iOS?

Page 9: IOS Swift language 1st Tutorial

What’s in iOS?

Page 10: IOS Swift language 1st Tutorial

Welcome to Xcode

Page 11: IOS Swift language 1st Tutorial

Welcome to Xcode

Page 12: IOS Swift language 1st Tutorial

Welcome to Xcode

Page 13: IOS Swift language 1st Tutorial

Welcome to Xcode

Page 14: IOS Swift language 1st Tutorial

Welcome to Xcode

Navigator

Page 15: IOS Swift language 1st Tutorial

Welcome to Xcode

Class Hierarchy

Page 16: IOS Swift language 1st Tutorial

Welcome to Xcode

Search in the Project

Page 17: IOS Swift language 1st Tutorial

Welcome to Xcode

Breakpoints in the Project for Debugging

Page 18: IOS Swift language 1st Tutorial

Welcome to Xcode

Previous Builds.

Page 19: IOS Swift language 1st Tutorial

Welcome to Xcode

This is Controller from where we would control our views

Page 20: IOS Swift language 1st Tutorial

Welcome to Xcode

This is our View where we would design our GUI

Page 21: IOS Swift language 1st Tutorial

Welcome to Xcode

This is our View where we would design our GUI with Drag and Drop not with code.

Page 22: IOS Swift language 1st Tutorial

Welcome to Xcode

Double click on white area will Zoom Out the GUI.

Page 23: IOS Swift language 1st Tutorial

Welcome to Xcode

In complex projects, we will have many of such square areas to design different screens and applications switch from one screen to another screen.This area would be like big map.

Page 24: IOS Swift language 1st Tutorial

Welcome to Xcode

This is utilities area.

Page 25: IOS Swift language 1st Tutorial

Welcome to Xcode

This is Object Library. These things make GUI and consist of lot of objects.

Page 26: IOS Swift language 1st Tutorial

Let’s make Calculator

To make the Result area of Calculator, we need a Label whose text would be updated with the result when we perform addition, subtraction, multiplication and Other mathematical functions.We shall drag and drop that Label on our View area.

Page 27: IOS Swift language 1st Tutorial

Let’s make Calculator

To make the Result area of Calculator, we need a Label, These Blue lines help me aligning the placement of Label or other GUI elements on the View Area.

Page 28: IOS Swift language 1st Tutorial

Let’s make Calculator

To make the Result area of Calculator, we need a Label, These boxes show that this Label is selected. You can change the size of Label by clicking and dragging side of the box.

Page 29: IOS Swift language 1st Tutorial

Let’s make Calculator

To make the Result area of Calculator, we need a Label, These boxes show that this Label is selected. You can change the size of Label by clicking and dragging side of the box.

Page 30: IOS Swift language 1st Tutorial

Let’s make Calculator

To make the Result area of Calculator, we need a Label, You can change the text Label by double clicking on Label.

Page 31: IOS Swift language 1st Tutorial

Let’s make Calculator

To make the Result area of Calculator, we need a Label, You have changed the Text Label to 0 by double clicking on Label.

Page 32: IOS Swift language 1st Tutorial

Let’s make Calculator

To make the Result area of Calculator, we need a Label, Click on Attributes Inspector. Then you can change the text alignment of 0 to Right Align.

Page 33: IOS Swift language 1st Tutorial

Let’s make Calculator

To make the Result area of Calculator, we need a Label, Click on Attributes Inspector. Then you can change the Font Size of 0 to 32.

Page 34: IOS Swift language 1st Tutorial

Let’s make Calculator

To make the Result area of Calculator, we need a Label, Click on Lower side of the Label Box to increase the height of the Label.

Page 35: IOS Swift language 1st Tutorial

Let’s make Calculator

To make the Result area of Calculator, we need a Label, Click on Lower side of the Label Box to increase the height of the Label.

Page 36: IOS Swift language 1st Tutorial

Let’s make Calculator

Let’s Run this Application. You can Run Application on any iOS device attached to Your computer.Or Run the Application on any Simulator listed below.I don’t have any device currently attached Therefore it would Run in Iphone 6s Plus simulator as shown selected in the image.

Page 37: IOS Swift language 1st Tutorial

Let’s make Calculator

Let’s Run this Application. You can Run Application by Clicking on Play Button.The application would Run in Iphone 6s Plus Simulator as shown.

Page 38: IOS Swift language 1st Tutorial

Let’s make Calculator

Now we are in Simulator, its loading now..This is separate application other than Xcode.

Page 39: IOS Swift language 1st Tutorial

Let’s make Calculator

Now we are in Simulator, it would ask for Proxy setting if used on UMT Network.

Page 40: IOS Swift language 1st Tutorial

Let’s make Calculator

Now we are in Simulator, it would not show full screen because my System resolution is lower as compared to Iphone 6s Plus. So we can scale down resolution to 50%.

Page 41: IOS Swift language 1st Tutorial

Let’s make Calculator

Now we are in Simulator, Now this shows full screen without Scroll on 50% resolution.

Page 42: IOS Swift language 1st Tutorial

Let’s make Calculator

Now we are in Simulator, This would be the display on Non-Proxy network.

But this is just White Screen Why???????????

Page 43: IOS Swift language 1st Tutorial

Let’s make Calculator

This Label is present there but this is Off screen. See its width.Its Square. No iOS device is Square.They are Rectangle in shape each one has different size and shape.Then why we design in Square???

Page 44: IOS Swift language 1st Tutorial

Let’s make Calculator

We design everything in Square and then give Rules called Constraints to everything inside square that define what happen to them when they Squished down horizontally or vertically.

Page 45: IOS Swift language 1st Tutorial

Let’s make Calculator

Now let us give some Rules to this label. That this should know what to do when this Label squished down in any direction for any device.

Page 46: IOS Swift language 1st Tutorial

Let’s make Calculator

Now let us give some Rules to this label. iOS wants us to use the whole width available so we would pin the right edge of Label Rectangle to the Square right edge.So they will be always pinned to each other.

Page 47: IOS Swift language 1st Tutorial

Let’s make Calculator

Now let us give some Rules to this label. How we would pin right edges together?Step 1: First Press control button then click and drag from right edge of Label to Square edge as shown in figure.

Page 48: IOS Swift language 1st Tutorial

Let’s make Calculator

Now let us give some Rules to this label. How we would pin right edges together?Step 2: When you would release the mouse click. It would show the little black box that asks which constraints to choose from the list of constraints

Page 49: IOS Swift language 1st Tutorial

Let’s make Calculator

Now let us give some Rules to this label. How we would pin right edges together?This would result after step 2 is completed.

Page 50: IOS Swift language 1st Tutorial

Let’s make Calculator

Now let us give some Rules to this label. How we would pin left edges together?Same 2 step process is repeated here for left edges.

Page 51: IOS Swift language 1st Tutorial

Let’s make Calculator

Now let us give some Rules to this label. How we would pin top edges together?Same 2 step process is repeated

Page 52: IOS Swift language 1st Tutorial

Let’s make Calculator

Now let us give some Rules to this label. How we would pin all edges together?This would be result when we pin right, left and top. But we didn’t pin bottom..

Page 53: IOS Swift language 1st Tutorial

Let’s make Calculator

Now let us give some Rules to this label. How we would pin top edges together?Same 2 step process is repeated

Page 54: IOS Swift language 1st Tutorial

Let’s make Calculator

You can hide Navigation and Utilities area by pressing these buttons to have this view in xcode.

Page 55: IOS Swift language 1st Tutorial

Let’s make Calculator

We don’t need this code in our Calculator.

Page 56: IOS Swift language 1st Tutorial

Let’s make Calculator

This is the Basic Class Declaration code in Swift. This class ViewController is being inhereted from its Superclass UIViewController class. Swift has single inheritance. Means one class can inheret from only one class.Don’t inheret from any class if you don’t want to.

Page 57: IOS Swift language 1st Tutorial

Let’s make Calculator

We are inheriting from UIViewController so that we can get all the mechanisms of controlling UI through this class in our own ViewController class.

We can inheret indirectly from UIViewController as well by inhereting from a class that inherets from UIView Controller.

Page 58: IOS Swift language 1st Tutorial

Let’s make Calculator

Our own ViewController class has a very generic name when you would make your own application then you would have a lot of these classes and Views so choose a better name like CalculatorViewController that looks more relevant to your application or any better name.

Page 59: IOS Swift language 1st Tutorial

Let’s make Calculator

To connect this UI with the code. We need to make Instance Variable (Property) so that we can update the value of Label whenever we want by changing the value of that Instance variable.

Lets connect UI with ViewController. First we would press Control button and click on Label and drag it into our class code.

Page 60: IOS Swift language 1st Tutorial

Let’s make Calculator

By releasing it would ask what kind of connection you want to make, We choose Outlet connection.Outlet connection is a instance Variable that points to this Label in UI.

Lets name it first.

Page 61: IOS Swift language 1st Tutorial

Let’s make Calculator

Lets name it first. We have named it display.

This would automatically understand that its type is UILabel because we made it by dragging it from Label to our class.

Page 62: IOS Swift language 1st Tutorial

Let’s make Calculator

This has made an @IBOutlet weak var display: UILabel!

Purpose of @IBOutlet is to make a coonection between UILabel and your code as shown in gutter.

Weak is for the purpose of Automatic reference counting (ARC)

Actual Syntax to create a Property or Instance variable is followingvar display: UILabel!

Page 63: IOS Swift language 1st Tutorial

Let’s make Calculator

Now we would put a Button on our View Screen.

Page 64: IOS Swift language 1st Tutorial

Let’s make Calculator

You can change the size of the button from Size Inspector.

Page 65: IOS Swift language 1st Tutorial

Let’s make Calculator

We have changed the Button Text to 7 increased its size and Font size of text on the button and would connect this button to UI the same way we did to our Label.

Page 66: IOS Swift language 1st Tutorial

Let’s make Calculator

Press Control button then click on Button and then drag mouse into the code then this screen would appear.

Page 67: IOS Swift language 1st Tutorial

Let’s make Calculator

When you would release mouse and Control button. This screen would appear with little box that would ask what would you want to connect.

This time we don’t want IBOutlet, we want Action so we would select Action from top drop down menu item.

Page 68: IOS Swift language 1st Tutorial

Let’s make Calculator

This would Ask for the name of the Action “appendDigit” and change the Type to “UIButton”

Page 69: IOS Swift language 1st Tutorial

Let’s make Calculator

This would Ask for the name of the Action “appendDigit” and change the Type to “UIButton”.

Press Connect button.

Page 70: IOS Swift language 1st Tutorial

Let’s make Calculator

This is the Syntax of the function definition.

func appendDigit(sender: UIButton)

Page 71: IOS Swift language 1st Tutorial

Let’s make Calculator

If function has return type then its is written after argument paraenthesis after arrow sign. As shown in the figure it is returning the Double.

func appendDigit(sender: UIButton, display: UILabel) -> Double.

Page 72: IOS Swift language 1st Tutorial

Let’s make Calculator

Page 73: IOS Swift language 1st Tutorial

Let’s make Calculator

Page 74: IOS Swift language 1st Tutorial

Let’s make Calculator

Page 75: IOS Swift language 1st Tutorial

Let’s make Calculator

Page 76: IOS Swift language 1st Tutorial

Let’s make Calculator

Page 77: IOS Swift language 1st Tutorial

Let’s make Calculator

Page 78: IOS Swift language 1st Tutorial

Let’s make Calculator

Page 79: IOS Swift language 1st Tutorial

Let’s make Calculator

Page 80: IOS Swift language 1st Tutorial

Let’s make Calculator

?

Page 81: IOS Swift language 1st Tutorial

Let’s make Calculator

Page 82: IOS Swift language 1st Tutorial

Let’s make Calculator

Page 83: IOS Swift language 1st Tutorial

Let’s make Calculator

Page 84: IOS Swift language 1st Tutorial

Let’s make Calculator

Page 85: IOS Swift language 1st Tutorial

Let’s make Calculator

Will Discuss this in Next Class.

Thanks