Picker View Control:

Source: Internet
Author: User

Picker View Control: Chinese: Single wheel selector custom selector is subject to two protocols: Data source Protocol and delegation protocol Uipickerview there is an instance method SelectRow: (Nsinteger) Incomponent: ( Nsinteger) Animated: (BOOL), which is a programmatic way to select a value. Uipickerview must implement some protocol in code to display properly, and cannot configure the appearance of the selector view in Attributes Inspector (property check)1The Data source Protocol Data source Protocol (Uipickerviewdatasource) is used to describe how much information the selector displays. There are two main methods: Numberofcomponentinpickerview--returns the number of components of a selector pickerview:numberofrowsincomponent--returns the number of rows (different input values) that the specified component contains, for example, if you want to create a selector that displays two columns, where the first column contains a value to choose from, and the second column contains two, the code is implemented as follows:@interfaceViewcontroller:uiviewcontroller <uipickerviewdatasource>-(Nsinteger) Numberofcomponentsinpickerview: ( Uipickerview *) pickerview{return 2;//Show two columns}-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (nsinteger) component{if(component==0)    {        //If this is the first column, 1 labels are displayed        return 1; }    Else    {        //Otherwise 2 labels are displayed        return 2; }}2The Trust Agreement delegation Agreement (UIPICKERVIEWDELEGATE) is responsible for creating and working with the series of selectors. It is responsible for passing the appropriate data to the selector for display and determining whether the user has made a choice. The protocol contains multiple methods, but only two are necessary: PickerView:titleForRow:forComponent--returns the title of the row according to the specified component and line number, which means that the string that should be displayed to the user is not implemented, and the selector caption will not be displayed, and will be run after? Replace. PickerView:didSelectRow:inComponent--When the user makes a selection in the selector, the delegate method is called, passing the line number selected by the user and the group that the user last touched Thing Uipickview also has an example method-(Nsinteger) Selectedrowincomponent: (Nsinteger) component, used to return the number of rows currently selected for the specified column. Usually used in conjunction with the pickerView:didSelectRow:inComponent. Imagine a situation in which the selector has two columns, the first column is the country, and the second column is the region. If you turn the country first, this time you want to choose the default region, you can only get through the selectedrowincomponent. (The row value that Didselectrow gets at this time is the country's column)@interfaceViewcontroller:uiviewcontroller <uipickerviewdatasource,uipickerviewdelegate>-(NSString *) PickerView: ( Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (nsinteger) component{if(component==0)    {        //if it is the first column, show "China"        return @"China"; }    Else    {        if(row==0)        {            return @"Beijing"; }        Else        {            return @"Hong Kong"; }    }}- (void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (nsinteger) component{if(component==0) {NSLog (@"the user selects the first column"); }    Else{NSLog (@"the user selects the second column"); NSLog (@"the user selected line%d", Row); }} There are some nonessential, but also useful methods: Pickerview:rowheightforcomponent--Returns (sets) the row height for the specified component in pixels pickerview:widthforcomponent--Returns (sets) the width of the specified component in pixels pickerView:viewForRow:viewForComponent:ReusingView--returns the custom view that should be displayed for the specified component and line number at the appropriate location, with a priority higher than pickerView:titleForRow:forComponent-(CGFloat) Pickerview: (Uipickerview *) Pickerview rowheightforcomponent: (nsinteger) component{if(component==0)    {        //set the row height of the first column        return  -; }    Else    {        return  -; }}-(CGFloat) Pickerview: (Uipickerview *) Pickerview widthforcomponent: (nsinteger) component{if(component==0)    {        //set the width of the first column        return  -; }    Else    {        return  the; }}-(UIView *) Pickerview: (Uipickerview *) Pickerview Viewforrow: (nsinteger) Row forcomponent: (Nsinteger) component Reusingview: (UIView *) view{if(component==0)    {        //The first column returns a label componentUILabel *label = [[UILabel alloc] Initwithframe:cgrectmake (0,0, the, +)]; Label.backgroundcolor=[Uicolor Greencolor]; Label.textcolor=[Uicolor Redcolor]; Label.text=@" First"; returnlabel; }    Else    {        //The second column returns a picture component        if(row==0)        {            return[[Uiimageview alloc] initwithimage:[uiimage imagenamed:@"Orange.gif"]]; }        Else        {            return[[Uiimageview alloc] initwithimage:[uiimage imagenamed:@"Strawberry.gif"]]; }    }}

Picker View Control:

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.