Examples of Uipickerview use in iOS development

Source: Internet
Author: User

The system's Uipickerview is very simple, the style is also very simple monotonous, the interface feeling is very monotonous not good-looking, sometimes needs us to customize, makes oneself wants the style. First give the generic style of the Uipickerview example, put the code:

#import "ZidingyipikViewController.h"

@interface Zidingyipikviewcontroller () <uipickerviewdelegate,uipickerviewdatasource>{
Record whether the wheel is sliding
NSString *guildstr;
NSString *selectstr;
Nsmutablearray *datamutarray;
UIButton *bgbutton;
}

@end

@implementation Zidingyipikviewcontroller

-(void) Viewdidload {
[Super Viewdidload];
Datamutarray = [Nsmutablearray arraywitharray:@[@ "student", @ "worker", @ "teacher", @ "security", @ "Doctor", @ "Nurse", @ "Waiter"]];
UIButton *button = [[UIButton alloc] Initwithframe:cgrectmake (80, 140, 70, 30)];
[Button settitle:@ "pop-up box" forstate:uicontrolstatenormal];
[Button Settitlecolor:[uicolor Whitecolor] forstate:uicontrolstatenormal];
Button.backgroundcolor = [Uicolor Orangecolor];
[Button addtarget:self action: @selector (Buttonaction) forcontrolevents:uicontroleventtouchupinside];
[Self.view Addsubview:button];
}
#pragma mark-Bomb frame
-(void) buttonaction{
Guildstr = @ "0";
Bgbutton = [[UIButton alloc] Initwithframe:cgrectmake (0, 0, Kscreenwidth, kscreenheight)];
Bgbutton.backgroundcolor = RGBA (0, 0, 0, 0.3);
[Bgbutton addtarget:self Action: @selector (bgbuttonaction:) forcontrolevents:uicontroleventtouchupinside];
[Self.view Addsubview:bgbutton];

UIView *cycanview = [[UIView alloc] Initwithframe:cgrectmake (0, kScreenHeight-180, Kscreenwidth, 40)];
Cycanview.backgroundcolor = [Uicolor Orangecolor];
[Bgbutton Addsubview:cycanview];

Uilabel *titlelabel = [[Uilabel alloc] Initwithframe:cgrectmake (0, 0, Kscreenwidth, cycanview.height)];
Titlelabel.text = @ "Select Identity";
Titlelabel.font = font (14);
Titlelabel.textcolor = [Uicolor Whitecolor];
Titlelabel.textalignment = Nstextalignmentcenter;
[Cycanview Addsubview:titlelabel];

UIButton *cancelbutton = [[UIButton alloc] Initwithframe:cgrectmake (0, 0,, Cycanview.height)];
[CancelButton settitle:@ "Cancel" forstate:uicontrolstatenormal];
[CancelButton Settitlecolor:[uicolor Whitecolor] forstate:uicontrolstatenormal];
CancelButton.titleLabel.font = font (14);
[CancelButton addtarget:self Action: @selector (bgbuttonaction:) forcontrolevents:uicontroleventtouchupinside];
[Cycanview Addsubview:cancelbutton];

UIButton *confirmbutton = [[UIButton alloc] Initwithframe:cgrectmake (kScreenWidth-48, 0, Cycanview.height)];
[ConfirmButton settitle:@ "determine" forstate:uicontrolstatenormal];
[ConfirmButton Settitlecolor:[uicolor Whitecolor] forstate:uicontrolstatenormal];
ConfirmButton.titleLabel.font = font (14);
[ConfirmButton addtarget:self Action: @selector (confirmbuttonaction:) forcontrolevents:uicontroleventtouchupinside ];
[Cycanview Addsubview:confirmbutton];

Uipickerview *selectpickerview = [[Uipickerview alloc] Initwithframe:cgrectmake (0, kScreenHeight-140, Kscreenwidth, 140)];
Show check box
Selectpickerview.showsselectionindicator = NO;
Selectpickerview.backgroundcolor = [Uicolor Whitecolor];
Selectpickerview.delegate = self;
Selectpickerview.datasource = self;
Selectpickerview.autoresizingmask = Uiviewautoresizingflexiblewidth;
[Bgbutton Addsubview:selectpickerview];
}
#pragma mark-Hidden bomb frame
-(void) Bgbuttonaction: (UIButton *) sender{
[Bgbutton Removefromsuperview];
}
#pragma mark-Bomb box OK button
-(void) Confirmbuttonaction: (UIButton *) sender{
if ([Guildstr isequaltostring:@ "0"]) {
SELECTSTR = [NSString stringwithformat:@ "%@", Datamutarray[0]];
}
[Self showhudtextonly:selectstr];
[Bgbutton Removefromsuperview];
}
#pragma Mark-uipickerview Proxy method
Number of Pickerview columns
-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) Pickerview {
return 1;
}
Number of Pickerview per column
-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (Nsinteger) Component {
return datamutarray.count;
}
Width per column
-(CGFloat) Pickerview: (Uipickerview *) Pickerview widthforcomponent: (Nsinteger) Component {
return kScreenWidth-85 * 2;
}
Returns the selected row
-(void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (Nsinteger) component
{
GUILDSTR = @ "1";
SELECTSTR = [NSString stringwithformat:@ "%@", [Datamutarray Objectatindex:row]];
NSLog (@ "selectstr:%@", selectstr);
}
Returns the contents of the current row, where the values in the array are added to the scrolling display bar
-(nsstring*) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (Nsinteger) component
{
return [Datamutarray Objectatindex:row];
}

Next, if you want to change the style, customize the Uipickerview style you want, just rewrite the method. Method:


Override method to change font size
-(UIView *) Pickerview: (Uipickerview *) Pickerview Viewforrow: (nsinteger) Row forcomponent: (Nsinteger) component Reusingview: (UIView *) view{
Uilabel *pickerlabel = (Uilabel *) view;
if (!pickerlabel) {
Pickerlabel = [[Uilabel alloc] init];
Pickerlabel.font = Font (17);
Pickerlabel.textcolor = [Uicolor blackcolor];
Pickerlabel.textalignment = 1;
[Pickerlabel Setbackgroundcolor:[uicolor Clearcolor]];
}
Pickerlabel.text = [self Pickerview:pickerview titleforrow:row forcomponent:component];
In the proxy method, add the following two lines of code to remove the black and down
[[Pickerview.subviews objectatindex:1] sethidden:yes];
[[Pickerview.subviews Objectatindex:2] sethidden:yes];

UIView *lineview1 = [[UIView alloc] Initwithframe:cgrectmake (n., kScreenWidth-85 * 2, 1.8)];
Lineview1.backgroundcolor = RGB (245, 245, 245);
[Pickerview Addsubview:lineview1];

UIView *lineview2 = [[UIView alloc] Initwithframe:cgrectmake (kScreenWidth-85 (2, 1.8)];
Lineview2.backgroundcolor = RGB (245, 245, 245);
[Pickerview Addsubview:lineview2];

return Pickerlabel;
}
Note:
Current screen width
#define Kscreenwidth [UIScreen mainscreen].bounds.size.width
Current screen height
#define Kscreenheight [UIScreen mainscreen].bounds.size.height

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.