Uipickerview selector (scroll wheel)-iOS development

Source: Internet
Author: User
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 */}

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.