Walkthrough iOS development for coder (Swift) – Updating — March 10, 2017

Walkthrough iOS development for coder (Swift) – Updating

This isn't tutorial, just be the collection of tutorials. If you wanna have  a walkthrough of iOS development, follow these steps. Please do not forget to complete attached assignments.

Step 1: Understand swift syntax

Tip: Just need to answer my above questions.

For more detailhttps://itunes.apple.com/us/book/the-swift-programming-language-swift-3-1-edition/id1002622538?mt=11

Step 2: Get started with UI

  • Pre: Try to create XCode project (Single view application)

  • 2.1 Simple elements:

    • I generally classify simple UI elements into 3 groups:
      • Not interact with user: UILabel, UIImageView
      • Simple UIControl handled by IBActionUIButton, Switch, Stepper, Slider, Segment…
      • Simple UIControl handled by delegate (and IBAction): UITextField, TextView, UIWebview v.v…
    • To getting started with these elements, you have to know:
      • How to create IBOutlet, what is IBOutlet?
      • How to create IBAction, what is IBAction, how to know if a control has IBAction or not?
      • How to implement delegate, and what for?

TIP: Just need to answer my above questions.

  • 2.2 Autolayout:

    • Autolayout: flexible layout UI element  with all of device sizes (have to practice autolayout for all of your assignment or demo, because it’s not difficult to use, but maybe make beginner confused.)
    • “Size-Classes”: Different layouts for different sizes of device.

TIP:

This is too hard for imaging so you’d better watch video instead of reading tutorial

Video: https://www.youtube.com/watch?v=7iT9fueKCJM

Document tutorial: https://www.appcoda.com/auto-layout-guide/

  • 2.3 Understand data type:

    • Single element: Int, Double, Float, String, Date, Bool, Data
    • Set of elements: Array, Dictionary
  • 2.4 ImageAsset

    • Understand difference between point and pixel in iOS
    • What are abc.png, abc@2x.png, abc@3x.png?

Assignment 01: Create an calculator like this:

IMG_1289

Step 3: Advance of UI element:

Assignment 02: Create karaoke application. Download the requirement and resources from here.

Step 4: Connect to server and local database

Assignment 03: Apply core data to karaoke application. Download the requirement and resources from here.

Assignment 04: Create food recipes using API. Download the requirement and resources from here.

Step 5: Advance

Localized an existed application (Part 1) — February 20, 2017

Localized an existed application (Part 1)

It’s used for simple applications that don’t have storyboard.

Step 1: Right click your project, then click “New File…”

Screen Shot 2017-02-16 at 11.06.28 PM.png

Choose “Strings File”, the file created MUST be named “Localizable.strings

Screen Shot 2017-02-16 at 11.06.45 PM.png

Step 2:

Turn on localized mode of this project: Select project, then click (+) at “Localizations” like this photo.Screen Shot 2017-02-20 at 11.59.17 PM.png

and add any language that your application wanna support.

Screen Shot 2017-02-16 at 11.07.45 PM.png

Then every StringFile will include language sub-files:

Screen Shot 2017-02-21 at 12.02.10 AM.png

You have to define key-value pair each file.

For example:

Localizable.strings (Vietnamese)

“login_screen_welcome” = “Xin chào, vui lòng đăng nhập “.

“login_screen_cancel” = “Hủy”.

Localizable.strings (Base – mean default)

“login_screen_welcome” = “Hi, please login to continue”.

“login_screen_cancel” = “Cancel”.

Step 3: 

Find all assigning string value in source code, then replace

[doneBtn setTitle:@”Cancel” forState:UIControlStateNormal];

=======>>>>>

[doneBtn setTitle:NSLocalizedString(@”login_screen_cancel“, @””) forState:UIControlStateNormal];

Step 4: Change language of your device and review it.

 

UITableView Tutorial (custom cell) — March 29, 2016

UITableView Tutorial (custom cell)

Step 1: Create Single View Application project.

Screen Shot 2016-04-06 at 9.59.28 PM

Step 2: Drag UITableView into view controller in Storyboard:

Screen Shot 2016-04-06 at 10.05.29 PM

Step 3: Create prototype cell.

A tableview can contain one or more prototype cells, it depends on how many kinds of cells in your tableview. In this example, there’s only one prototype cell.

a/ To create prototype cell, increase number of prototype cells in Attribute Inspector bar:

Screen Shot 2016-04-06 at 11.02.50 PM

b/ Drag subviews to prototype cell like this:

Screen Shot 2016-04-06 at 11.29.02 PM

c/ Set reuse identifier for cell:

Click to focus the cell (1) then set identifier for cell (2). Cell identifier is used to identify prototype cells in tableview. (note that cell’s style is Custom).

Screen Shot 2016-04-06 at 11.37.28 PM

Tableview will create a new cell for each identifier that you have registered, they are stored in tableview’s “FREE BIN”.

When you call method below, tableview gives you an available cell,  you can fill data to this cell if needed. In case there’s no available cell in “FREE BIN”, it will be created automatically. When user scrolls up or down the tableview, the invisible cell will be moved to “free bin”. So at moment, the number of UITableViewCell’s instance is limited, good for application memory.

- (UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;

After completed 3 above steps, press command + R to run:

Simulator Screen Shot Apr 6, 2016, 11.47.43 PM

Step 4: Create custom class of UITableViewCell

Because the food prototype cell is a custom cell, you need to create a new class inherited from UITableViewCell. This custom class is used to create outlets for views dragged at step 3b.

To create custom cell of UITableViewCell: File -> Add -> New File -> Cocoa Touch Class:

Screen Shot 2016-04-10 at 10.02.37 AM

(1) : Name of custom class.

(2): Super class of your custom class, must be UITableViewCell in this case.

Then, you need to register custom class for prototype cell in storyboard:

Screen Shot 2016-04-10 at 10.09.00 AM

c/ Create outlet for FoodTableViewCell:

Screen Shot 2016-04-10 at 10.13.46 AM

 

Step 5: After creating tableview and prototype cell, next step is filling data into this tableview.

To fill data to tableview, you should implement UITableViewDelegate and UITableViewDatasource, answering 3 important questions (equivalent to 3 important methods in datasource).

  • How many sections are there in this table view? (Optional, default is 1 section)
  • How many rows are there in each section?
  • With an IndexPath, which cell(s) do you wanna show? (IndexPath has section and row properties).

For example, the data you need to fill to tableview are 2 arrays: one of foods’ name and one of thumbnails:

self.foodNames = @[@"Seafood pizza", @"Egg Hamburger", @"Salad", @"Hot green tea", @"Coke", @"Chicken combo", @"Family combo", @"Friends Set", @"Couple set", @"Ice cream"]; 

self.foodThumbnailNames = @[@"img_pizza.jpg", @"img_ham_egg.jpg", @"img_salad.jpg", @"img_tea.jpg", @"img_coke.jpg", @"img_combo1.jpeg", @"img_family_set.jpg", @"img_friend_set.jpg", @"img_couple_set.jpg", @"img_ice_cream.jpg"];

Note: foodNames and foodThumbnailNames are properties (@property) of ViewController class.

a/ Set delegate, datasource of tableview:

Right click tableview then drag datasource and delegate outlets to ViewController like this:

Screen Shot 2016-04-06 at 11.54.35 PM

b/ Add UITableViewDelegate and UITableViewDatasource to @interface of class:

Screen Shot 2016-04-06 at 11.55.16 PM

c/ Implement 3 important methods:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
     return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
      return self.foodNames.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
      FoodTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FoodCell"];
      NSString *foodName = self.foodNames[indexPath.row];
      NSString *foodThumbnail = self.foodThumbnailNames[indexPath.row];
 
      cell.nameLabel.text = foodName;
      cell.thumbnailImageView.image = [UIImage imageNamed:foodThumbnail];
      cell.priceLabel.text = [NSString stringWithFormat:@"$%zd", arc4random() % 10 + 5]; //price from: $5 -> $15 (random)
      cell.noteLabel.text = [NSString stringWithFormat:@"Please wait: %zd mins", arc4random() % 59 + 1]; //waiting from 1 -> 59s.(random)
      return cell;
}

Press Command + R to run:

Simulator Screen Shot Apr 10, 2016, 10.27.05 AM

 

 

Summary:

  • The most important step to implement is Step 5. You should learn by heart 3 important methods of UITableViewDatasource.
  • The reuse system of UITableView is very good for iOS application, make implementation easier for dev.
  • You need to understand clearly purposes of every step.