093 adding a UIView subclass to the selector

Source: Internet
Author: User

The effect is as follows:

ViewController.h

1 #import <UIKit/UIKit.h>23@interface Viewcontroller:uiviewcontroller <uipickerviewdatasource, uipickerviewdelegate>4 @property (Strong, Nonatomic) Uipickerview *  Pikvcustom; 5 @property (Strong, Nonatomic) Nsarray *arranimal; 6 7 @end

Viewcontroller.m

1 #import "ViewController.h"2 3 @interfaceViewcontroller ()4- (void) Layoutui;5- (void) Buttondidpush: (UIButton *) sender;6 @end7 8 @implementationViewcontroller9 Ten- (void) Viewdidload { One [Super Viewdidload]; A      - [self layoutui]; - } the  -- (void) didreceivememorywarning { - [Super didreceivememorywarning]; -     //Dispose of any resources the can be recreated. + } -  +- (void) Viewwillappear: (BOOL) Animated { A [Super viewwillappear:animated]; at [Self.navigationcontroller setnavigationbarhidden:no animated:animated]; - [Self.navigationcontroller settoolbarhidden:no animated:animated]; - } -  -- (void) Layoutui { -Self.view.backgroundColor =[Uicolor Whitecolor]; inSelf.navigationItem.title =@"to add a UIView subclass to the selector"; -      to     //initialize an array of animal images (data source) _arranimal +UIImage *imgdog = [UIImage imagenamed:@"Dog"]; -UIImage *imgelephant = [UIImage imagenamed:@"Elephant"]; theUIImage *imglion = [UIImage imagenamed:@"Lion"]; *UIImage *imgmonkey = [UIImage imagenamed:@"Monkey"]; $_arranimal =@[imgdog, Imgelephant, Imglion, Imgmonkey];Panax Notoginseng_arranimal =@[_arranimal, _arranimal, _arranimal]; -      the_pikvcustom = [[Uipickerview alloc] Initwithframe:cgrectmake (0,0,0,0)]; +Cgpoint Newpoint =Self.view.center; A_pikvcustom.center =Newpoint; the_pikvcustom.datasource =Self ; +_pikvcustom.Delegate=Self ; - [Self.view Addsubview:_pikvcustom]; $      $UIButton *btnchoice = [[UIButton alloc] Initwithframe:cgrectmake (0,0, Max, +)]; -Newpoint.y + = the; -Btnchoice.center =Newpoint; theBtnChoice.layer.masksToBounds =YES; -BtnChoice.layer.cornerRadius =10.0;WuyiBtnChoice.layer.borderColor =[Uicolor Blackcolor]. Cgcolor; theBtnChoice.layer.borderWidth =2.0; -[Btnchoice Settitle:@"Selection Complete"Forstate:uicontrolstatenormal]; Wu [Btnchoice Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal]; - [Btnchoice addtarget:self About Action: @selector (Buttondidpush:) $ Forcontrolevents:uicontroleventtouchupinside]; - [Self.view Addsubview:btnchoice]; - } -  A- (void) Buttondidpush: (UIButton *) Sender { +Nsinteger numberofcomponents =[_pikvcustom numberofcomponents]; theNsmutablearray *marrnew =[[Nsmutablearray alloc] initwithcapacity:numberofcomponents]; -      for(Nsinteger i=0; i<numberofcomponents; i++) { $Uiimageview *IMGV = (Uiimageview *) [_pikvcustom viewforrow:[_pikvcustom selectedrowincomponent:i] forcomponent:i]; theUiimageview *imgvnew =[[Uiimageview alloc] initWithImage:imgV.image]; theUibarbuttonitem *barbtnnew =[[Uibarbuttonitem alloc] initwithcustomview:imgvnew]; the [Marrnew addobject:barbtnnew]; the     } - [self settoolbaritems:marrnew animated:yes]; in } the  the #pragmaMark-pickerview About-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) Pickerview { the     return[_arranimal Count];//returns the number of component columns the } the  +-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (Nsinteger) component { -     return[_arranimal[0] Count];//returns the number of component rows the }Bayi  the-(UIView *) Pickerview: (Uipickerview *) Pickerview Viewforrow: (nsinteger) Row forcomponent: (Nsinteger) component Reusingview: (UIView *) View { the     //Direct Display (reuse) if the parameter view object is already initialized -Uiimageview *IMGV = (Uiimageview *) view; -     if(!IMGV) { theUIImage *img =_arranimal[component][row]; theIMGV =[[Uiimageview alloc] initwithimage:img]; the     } the     returnIMGV; - } the  the @end

AppDelegate.h

1 #import <UIKit/UIKit.h>23@interface Appdelegate:uiresponder < Uiapplicationdelegate>4 @property (Strong, Nonatomic) UIWindow *window; 5 @property (Strong, Nonatomic) Uinavigationcontroller *Navigationcontroller; 6 7 @end

Appdelegate.m

1 #import "AppDelegate.h"2 #import "ViewController.h"3 4 @interfaceappdelegate ()5 @end6 7 @implementationappdelegate8 9-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {Ten_window =[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]; OneViewcontroller *viewcontroller =[[Viewcontroller alloc] init]; A_navigationcontroller =[[Uinavigationcontroller alloc] initwithrootviewcontroller:viewcontroller]; -_window.rootviewcontroller =_navigationcontroller; -     //[_window Addsubview:_navigationcontroller.view];//when _window.rootviewcontroller is associated, this sentence is optional the [_window makekeyandvisible]; -     returnYES; - } -  +- (void) Applicationwillresignactive: (UIApplication *) Application { - } +  A- (void) Applicationdidenterbackground: (UIApplication *) Application { at } -  -- (void) Applicationwillenterforeground: (UIApplication *) Application { - } -  -- (void) Applicationdidbecomeactive: (UIApplication *) Application { in } -  to- (void) Applicationwillterminate: (UIApplication *) Application { + } -  the @end

093 Adding a UIView subclass to the selector

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.