Basic view of iOS development--uipickerview

Source: Internet
Author: User
Tags uicontrol

Uipickerview components like HTML are all Select component effects, providing data for user selection. Data can be provided through the plist file. Uipickerview is a selector control that can generate a single-column selector, or a multicolumn selector, and the developer can customize the appearance of the selection and is flexible to use. Uipickerview directly inherits the UIView, does not inherit Uicontrol, so it cannot bind an event-handling method like Uicontrol, and the Uipickerview event processing is done by its delegate object.


Example one-single-row selector

viewcontroller.m//uipickerviewdemo////Created by Apple on 16/5/17.//copyright©2016 year. All rights reserved.//#import "ViewController.h" @interface viewcontroller () @endNSArray * timors; @implementation        viewcontroller-(void) viewdidload {[Super viewdidload];    Creates and initializes a Nsarray object.        Timors = [Nsarray arraywithobjects:@ "Titus 1" @ "Titus 2" @ "Titus 3" @ "Titus 4" @ "Timothy 5", Nil];    uipickerview* Pickerview = [[Uipickerview alloc] Initwithframe:cgrectmake (0, 200, 320, 200)];    Set DataSource and delegate for Uipickerview controls [Pickerview setdatasource:self];    [Pickerview setdelegate:self]; Set default selected value//SelectRow represents the default value for drop-down list (starting from 0)///Incomponent represents the first few drop-down list objects (starting from 0)//NOTE: Data initialization is done before the following code is not displayed [P        Ickerview selectrow:1 incomponent:0 Animated:yes];    [Self.view Addsubview:pickerview]; } #pragma the method defined in mark-uipickerviewdatasource//Uipickerviewdatasource, the settings appear in several drop-down lists (returning 2 indicates that 2 drop-down lists appear)-(Nsinteger) Numberofcomponentsinpickerview: (uipickerview*) Pickerview{return 1;} A method that is defined in Uipickerviewdatasource that determines how many list items the control specifies the column contains-(Nsinteger) Pickerview: (Uipickerview *) Pickerviewnumberofrowsincomponent: (Nsinteger) component{//Because the control contains only one column, there is no need to ignore the column ordinal parameter component// The method returns Timors.count, indicating how many elements the books contains, and how many rows the control contains to return timors.count;} Fires the method when the user selects the specified column in Uipickerviewdatasource-(void) Pickerview: (Uipickerview *) Pickerview Didselectrow: ( Nsinteger) Row incomponent: (nsinteger) component{//Use a uialertview to display the list item selected by the user uialertview* alert = [[Uialertview All                                    OC] initwithtitle:@ "hint" message:[nsstring stringwithformat:@ "your chosen Titus is:%@"                          , [Timors Objectatindex:row]] Delegate:nil    cancelbuttontitle:@ "determine" otherbuttontitles:nil]; [Alert show];} #pragma the method defined in mark-uipickerviewdelegate//uipickerviewdelegate, the NSString returned by the method is used as the caption text for the specified column, specified list item in the uipickerview//-( NSString *) Pickerview: (Uipickerview *) pickErview Titleforrow: (nsinteger) Row forcomponent: (nsinteger) component{//Because the control contains only one column, you do not need to ignore the column ordinal parameter component The method returns the elements in the timors based on the row parameter, which represents the number of the list item,//So that the method represents the first few list items, and uses the first element in Timors return [Timors Objectatindex:row];} @end

As follows:




Example two--Multi-column Selector

viewcontroller.m//multpickerviewdemo////Created by Apple on 16/5/17.//copyright©2016 year. All rights reserved.//#import "ViewController.h" @interface viewcontroller () @endNSArray * heroes;        nsarray* timors; @implementation viewcontroller-(void) viewdidload {[Super viewdidload];    Create and initialize 2 Nsarray objects, respectively, as 2 columns of data heroes = [Nsarray arraywithobjects:@ "Li Qing", @ "Titus", Nil];        Timors = [Nsarray arraywithobjects: @ "Titus 1" @ "Titus 2" @ "Titus 3" @ "Titus 4" @ "Timothy 5", Nil];    uipickerview* Pickerview = [[Uipickerview alloc] Initwithframe:cgrectmake (0, 200, 320, 200)];    Set DataSource and delegate for Uipickerview controls [Pickerview setdatasource:self];        [Pickerview setdelegate:self];    [Self.view Addsubview:pickerview]; } #pragma the method defined in mark-uipickerviewdatasource//Uipickerviewdatasource, the settings appear in several drop-down lists (returning 2 indicates that 2 drop-down lists appear)-(Nsinteger) Numberofcomponentsinpickerview: (uipickerview*) pickerview{return 2;} Uipickerviewdatasource the number of method settings defined in the drop-down list//parameter 1: Action of the Uipickerview object//Parameter 2: The subscript of the drop-down list for the Uipickerview object of the operation (starting from 0)-(Nsinteger) Pickerview: (Uipickerview *) Pickerviewnumberofrowsincomponent: ( Nsinteger) component{///If it is the first column, returns the number of elements in the authors//that is, how many elements the authors contains, and how many list items the first column contains if (component = = 0) {retur    n Heroes.count;    }//If it is a different column, returns the number of elements in the books. That is, how many elements are included in the books, and the other columns (only the second column) contain the number of list items return timors.count;} Fires the method when the user selects the specified column in Uipickerviewdatasource-(void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (    Nsinteger) Row incomponent: (nsinteger) component{nsarray* tmp = component = 0? heroes:timors; nsstring* tip = Component = = 0?    @ "Hero": @ "Titus";                          Use a uialertview to display user-selected list items uialertview* alert = [[Uialertview alloc] initwithtitle:@ "hint" Message:[nsstring stringwithformat:@ "Your selected%@ is:%@,", Tip, [tmp ob                          Jectatindex:row]] delegate:nil cancelbuttontitle:@ "OK"   Otherbuttontitles:nil]; [Alert show];} #pragma mark-uipickerviewdelegate//uipickerviewdelegate defined in the Method settings drop-down list of data//parameter 1: operation of the Uipickerview object// Parameter 2: drop-down list item subscript (starting from 0) [Number of items in drop-down list]//parameter 3: Subscript of the drop-down list of the Uipickerview object of the action [first drop-down list] (starting from 0)-(NSString *) Pickerview: (Uipickerview * ) Pickerview Titleforrow: (nsinteger) Row forcomponent: (nsinteger) component{//If it is the first column, return the element at row index in Heroes/    /That is, the list item header of the first column is determined by the Heroes collection element.    if (component = = 0) {return [Heroes Objectatindex:row];        }else{//If it is a different column (only the second column), returns the element at the row index in Timors//That is, the second column of the list item header is determined by the books collection element.    return [Timors Objectatindex:row]; }}//the method defined in Uipickerviewdelegate, the NSString returned by the method will be the width of the specified column in//Uipickerview-(CGFloat) Pickerview: (Uipickerview *)    Pickerview widthforcomponent: (nsinteger) component{//If it is the first column, the width is if (component = = 0) {return 90; }//If it is a different column (only the second column), the width is the return 210;} @end

As follows:



Example 3-interdependent multi-column Selector

viewcontroller.m//multpickerview2demo////Created by Apple on 16/5/17.//copyright©2016 year. All rights reserved.//#import "ViewController.h" @interface viewcontroller () @endNSArray * heroes; nsdictionary* skins;//Selectedhero Save the currently selected hero nsstring* Selectedhero; @implementation viewcontroller-(void)        viewdidload {[Super viewdidload];    Creates and initializes a Nsdictionary object.             Skins = [Nsdictionary dictionarywithobjectsandkeys: [nsarray arraywithobjects:@ "Li Qing 1" @ "Li Qing 2", nil] @ "Li Qing", [Nsarray arraywithobjects:@ "Titus 1", @ "Titus 2", Nil], @ "Titus", [Nsarray arraywithobjects:@ "Raven 1"    , @ "Raven 2", @ "Raven 3", Nil], @ "Raven", Nil];    Use heroes to save skins of all key nsarray sorted results heroes = [Skins AllKeys];        Sets the default selected author heroes the first element Selectedhero = [Heroes objectatindex:0];    uipickerview* Pickerview = [[Uipickerview alloc] Initwithframe:cgrectmake (0, 200, 320, 200)]; Set DataSource and delegate for Uipickerview controls [Pickerview SETDATasource:self];        [Pickerview setdelegate:self];    [Self.view Addsubview:pickerview]; } #pragma the method defined in mark-uipickerviewdatasource//Uipickerviewdatasource, the settings appear in several drop-down lists (returning 2 indicates that 2 drop-down lists appear)-(Nsinteger) Numberofcomponentsinpickerview: (uipickerview*) pickerview{return 2;} Uipickerviewdatasource the number of method settings defined in the drop-down list//parameter 1: operation of the Uipickerview object//Parameter 2: The subscript of the drop-down list of the action's Uipickerview object (starting from 0)-(nsinteger ) Pickerview: (Uipickerview *) Pickerviewnumberofrowsincomponent: (Nsinteger) component{//If it is the first column, returns the number of elements in the heroes//he    ROEs contains the number of elements, the first column contains the number of list items if (component = = 0) {return heroes.count;    }//If it is a different column (only the second column),//Returns the number of elements in the selectedhero corresponding nsarray in skins. return [[Skins Objectforkey:selectedhero] count];} Fires the method when the user selects the specified column in Uipickerviewdatasource-(void) Pickerview: (Uipickerview *) Pickerview Didselectrow: ( Nsinteger) Row incomponent: (Nsinteger) component{if (component = = 0) {//Change the selected Hero Selectedhero = [Heroe        s objectatindex:row]; In this case, when the upper layer is selected, the corresponding underlying data will change,Need reloadcomponent//control rewrite to load the second list, according to the selected author to load the second list [Pickerview reloadcomponent:1]; } nsarray* tmp = component = = 0?    Heroes: [Skins Objectforkey:selectedhero]; nsstring* tip = Component = = 0?    @ "Hero": @ "Skin";                          Use a uialertview to display user-selected list items uialertview* alert = [[Uialertview alloc] initwithtitle:@ "hint" Message:[nsstring stringwithformat:@ "Your selected%@ is:%@,", Tip, [tmp ob                          Jectatindex:row]] delegate:nil cancelbuttontitle:@ "OK"    Otherbuttontitles:nil]; [Alert show];} #pragma mark-uipickerviewdelegate//uipickerviewdelegate defined in the Method settings drop-down list of data//parameter 1: operation of the Uipickerview object// Parameter 2: drop-down list item subscript (starting from 0) [Number of items in drop-down list]//parameter 3: Subscript of the drop-down list of the Uipickerview object of the action [first drop-down list] (starting from 0)-(NSString *) Pickerview: (Uipickerview * ) Pickerview Titleforrow: (nsinteger) Row forcomponent: (nsinteger) component{//If it is the first column, return the element at row index in Heroes/ /That is, the element of the first column consists of heroThe ES collection element is determined.    if (component = = 0) {return [Heroes Objectatindex:row]; }//If it is another column (only the second column),//Returns the element at the row index in Selectedhero corresponding nsarray in Skins return [[Skins Objectforkey:selectedhero] Objecta Tindex:row];} The method that is defined in Uipickerviewdelegate, which returns nsstring as the width of the specified column in//Uipickerview-(CGFloat) Pickerview: (Uipickerview *)    Pickerview widthforcomponent: (nsinteger) component{//If it is the first column, the width is if (component = = 0) {return 90; }//If it is a different column (only the second column), the width is the return 210;} @end

As follows:


Basic view of iOS development--uipickerview

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.