Learn UI datasets (time selection) on iOS
We want to learn how to use the UI datasheet in iOS by giving an example in swift. We also learn with an example how to select the date, time or both items in the label; Use the iOS ui date sorter control in swift.
Database UI on iOS
In iOS, Datapark is a control that is selected to select a required date, time, or both, and also provides an interface for a countdown timer. Databases have different modes that can be used to specify date and time display formats based on the same requirements as date only display or time display only or date and time display. If we use the body of iOS history in our applications, which is similar to the following figure:
Here are some of the different modes available in the iOS Database:
Fashion | Definition |
Date | Represents months, days and years |
Time | Represents hours, minutes and an AM / PM design |
date and time | Indicates date, time; Minutes and an AM / PM design |
Countdown timer | Displays hours and minutes for a maximum of 23 hours and 59 minutes |
Now with an example, we see how to view the datasheet in iOS apps.
Create an iOS dating app in Swift
To create a new project in Xcode on iOS, open Xcode from the application folder list. After opening Xcode, the welcome window will open as shown below. In the Welcome window, click on the second option, “Create a new Xcode Project”; Click (create a new Xcode Project) or select the File New Project path.
After selecting “Create a new Xcode project”, a new window will open in which we must select our template.
The new Xcode window includes several built-in application templates to implement the usual type of iOS apps, such as page-based apps, tab-based apps, games, spreadsheet apps, and more. These templates have a preset interface and source code files. .
For example, in iOS, we have the most basic pattern of the app, which is “demo only”; We will use. To select this item, go to the iOS section on the left, select the application from the select section, and in the main part of the window that opens, select “single view application” and click on the next button, as shown below. .
After clicking Next, we will see a window like the one below, in this case we have to mention the project name and other details for our program.
Product name: “Image View in iOS”
The name we enter in the Product Name section is used for the project and application.
Organization name: “Tutlane”
You can enter the name of the organization or your name in this field; Of course you can leave that section blank.
Organization Identifier): “com.developersocociety)”
If you do not have an enterprise ID, enter com.example.
Bundle Identifier
This section is automatically generated based on the phrases we entered in the product name and organization ID.
Language: “Swift”
Select the language type “Swift” because we want to develop applications using swift.
Universal (devices): “Universal”
Select the Devices option as Universal This means that this app is for all Apple devices; If you need to run the app for iPad only, you can select the iPad option to restrict your app to running only on iPad devices.
Use core Data: Not selected
This option is used for database operations. Select this option if you are doing any database-related operations in your application, otherwise do not select this option.
Include Unit Test: Not selected
If you need unit tests for your application, select this option otherwise leave it unselected.
Includes UI tests: not selected
Select this option if you need UI tests for your application, otherwise do not select it.
After completing the options, click the Next button as shown below.
When we click the Next button; A new window will open in which you must use the location where you want the new project to be saved; Let’s choose. Once you have selected the new project storage location; As shown below; Click the Create button.
After clicking the Create button, Xcode opens and creates a new project. In our project, Main.storyboard and ViewController.swift are the main files used to design the user interface and maintain the source code.
Main.storyboard – which is the visual interface editor and this file is used to design the application interface.
ViewController.swift – which contains the source code of our application and we use this file to write any code related to our application.
Now select the Main.storyboard file in the project, until Xcode opens the visual interface editor as shown below.
Now open the ViewController.swift file in your project, which will look like the following figure.
Add UI controls to iOS for display in Swift
We are now adding controls to our application for that library of available objects. The object library appears at the bottom right of Xcode. If you did not find the object library, as shown below; Click the button that is the third button in the library selection bar on the left. (On the other hand, you can select the View Utilities Show Object Library path).
As mentioned earlier; Our interface is in the Main.storyboard file so open the Main.storyboard file. Now search for the object library in the Date Picker filter field, then add the label control to the ViewController in the Main.Storyboard in the same way as shown below; Add the Date Picker control.
Relate iOS UI controls to coding
Now we create the connection between the controls and the code ViewControlller.Swift. For this purpose, click on the Assistant button (two overlapping circles) which is located in the Xcode toolbar as shown below.
To map controls; Hold down the Ctrl key on the keyboard and as you can see below; Then drag the datasheet and label controls from the interface and drop it in the ViewController.swift file.
When we add controls to the ViewController.swift file; Then we write the custom code to show the date view and select the date and time from it. Once we have written all the functions we need; The code for the ViewController.swift file should look like this:
import UIkit
class ViewController: UIViewController {
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var datePicker: UIDatePicker!
Override func viewDidLoad () {
Super.viewDidLoad ()
datePicker.addTarget (self, action: #selector (ViewController.datePickerC hanged (_ :)), forControlEvents: UIContolEvents.ValueChanged)
}
let dateFormatter = NSDateFormatter ()
dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle
dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle
let strDate = dateFormatter.stringFromDate (datePicker.date)
dateLabel.text = strDate
}
Override func didReceiveMemorywarning () {
Super.didRecieveMemoryWarning ()
// Dispose of any resources that can be recreated.
}
}
Now run the program and check its output. To run the program, select the desired emulator (in this section we have selected the iPhone Six S Plus) and then click on the Play button, which is located in the Xcode toolbar in the upper left corner, as shown below.
Output UI app on iOS
In the following, you can see the result of running the Dat Body program. You can now select any date you want and that date will be displayed in the label control.
In this way, we can use the ui datasheet in iOS in Swift language to select the date and time from the datacon control based on our needs.