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