Swift UI special training-Pcikerview slots View

Source: Internet
Author: User

Slot machines believe that we all know, like a set of rollers, and we select one of the set of values. This is how the iOS clock is designed when you choose a time. Drag a Pickerview in the storyboard to show the following:


Pickerview is not able to choose the default value, the figure is a few characters are related to the name of Apple, if you want to set we can only go to the code settings. Let's make a slot machine with three parts, each with a label below it to show the selection. Drag-and-drop to the controller to establish the connection:



Then go back to Storyboard, select Pickerview, and connect the outlets on the right with the Viewcontroller:


So where does the data come from, we go back to the code, and we still need to manually add the inherited protocol:


Plus you'll notice an error, because you don't implement its callback method, just like TableView, if the controller inherits the data source, you have to implement methods that specify the number of rows and so on.

First, define an array of display content:

Let citys = ["Beijing", "Shanghai", "Guangzhou"]
Then define the callback method:

Func Numberofcomponentsinpickerview (Pickerview:uipickerview), Int {        return citys.count    }        func Pickerview (Pickerview:uipickerview, Numberofrowsincomponent component:int), Int {        return citys.count    }< C5/>func Pickerview (Pickerview:uipickerview, Titleforrow row:int, Forcomponent component:int), String! {        return citys[row]    }

Each represents a three-part, three-row, each-section header. The results are as follows:



Now let's add the real-world features of the tags and let them show the cities we selected in the slots:

Func Pickerview (Pickerview:uipickerview, Didselectrow row:int, incomponent component:int) {        switch component{ Case        0:label0.text = citys[row] case        1:label1.text = Citys[row]        default:label2.text = Citys[row]        }
   }//selected line which part

Effect




Swift UI special training-Pcikerview slots View

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.