Statement You are welcome to repost this article, but please respect the author's Labor achievements. repost this article and keep the statement in this box. Thank you. Article Source: http://blog.csdn.net/iukey |
The selector is used to replace the drop-down menu on the PC. It is a large scroll wheel and occupies a fixed size of 320 × 216.
I. Create
Although the size is fixed, the position can be arbitrary (but the horizontal is filled, we can only change the vertical position ). Similar to uitableview, The uipickerview class also requires a data source. Unlike table view, the selector uses an nsinteger value instead of an index path to identify each row. The selector can have multiple dials, each of which can be referenced as a component.
The selector view uses a proxy as the data source, so the data source can exist in another class or view controller.
UIPickerView* pickerView = [ [ UIpickerView alloc] initWithFrame:CGRectmake(0.0,100.0,0.0,0.0)];pickerView.delegate = self;pickerView.dataSource = self;
Ii. Attributes
The disgusting apple retains many attributes as private ones and opens few poor ones to developers. The only available appearance option is the selection window.
Set the showsselectionindicator attribute of the selector to yes. A transparent window is displayed on the current selection:
pickerView.showsSelectionIndicator = YES;
Iii. Data sources
The data source must be taken into account when creating the interface. These methods are necessary for the uipickerviewdatasource protocol.
Numberofcomponentsinpickerview
Wheel count
Numberofrowsincomponent
Set different numbers of rows for each scroll wheel in the selector. This method returns the number of rows of the specified scroll wheel.
In addition, the uipickerviewdelegate protocol implements the following methods to obtain the specific information of the selector component.
Titleforrow
Returns the actual dial value for the given row of the corresponding scroll wheel (component. These values are returned as nsstring objects.
Viewforrow
This method can overwrite the default behavior of the selector to display the dial of the component, so that any uiview class can be displayed in it.
Widthforcomponet
Returns the width of a specified widget (scroll wheel. If this method is not implemented, the selector automatically adjusts it to the appropriate width.
Rowheightcomponent
Returns the height of a specified widget (scroll wheel. If this method is not implemented, the selector automatically adjusts it to the appropriate height.
Iv. Display
[Self. View addsubview: pickerview];
5. Read Selector
The selectedrowincomponent method of the view is the most direct method for obtaining the index of the selected column in the selector view:
int selectedRow = [ pickerView selectedRowInComponent:0 ];
There is also a delegate method. When you select a row in the selector, you will receive a notification. This method can be used to send an alert to the object so that it can respond to the new line:
-(Void) pickerview :( uipickerview *) pickerview didselectedrow :( nsinteger) Row incomponent :( nsinteger) component {/* Add code and perform corresponding operations based on the selected row */}