Common options < Data source protocols, delegation agreements > (iOS development)

Source: Internet
Author: User

-The common choice must be fulfilled by the two protocols that are agreed to implement. One for the data source protocol

-the Trust agreement is responsible for controlling the UIof the control, event response , and implementing optional

-The data source protocol is responsible for the bridge between the control and the application data model, generally must implement


@interface Viewcontroller:uiviewcontroller <uipickerviewdelegate, uipickerviewdatasource>



  viewcontroller.h//  pickviewsample////  Created by Li Yakun on 14-10-21.//  Copyright (c) 2014 Li Yakun. All rights reserved.//#import <UIKit/UIKit.h> @interface viewcontroller:uiviewcontroller< Uipickerviewdelegate, uipickerviewdatasource>//Ordinary selectors must satisfy both protocols, one for the trust agreement, one for the data source Protocol//Trust protocol to control the control UI, the event response, to implement optional// The data source protocol is responsible for the bridge between the control and the application data model, generally must implement @property (weak, nonatomic) Iboutlet Uipickerview *pickerview; @property (weak, nonatomic) Iboutlet UILabel *label; @property (nonatomic, strong) nsdictionary *pickerdata;     Save All data @property (nonatomic, strong) Nsarray *pickerprovincesdata; Current provincial data @property (Nonatomic, strong) Nsarray *pickercitiesdata;    City data below current province-(ibaction) onclick: (id) sender; @end


viewcontroller.m//pickviewsample////Created by Li Yakun on 14-10-21.//Copyright (c) 2014 Li Yakun. All rights reserved.//#import "ViewController.h" @interface Viewcontroller () @end @implementation viewcontroller-(void    ) Viewdidload {[Super viewdidload];        Additional setup after loading the view, typically from a nib.    Locate the folder to specify the file and return the file path nsbundle *bundle = [NSBundle mainbundle]; NSString *plistpath = [Bundle pathforresource:@ "Provinces_cities" oftype:@ "plist        "];    Get all the data in the attribute list file nsdictionary *dict = [[Nsdictionary alloc] initwithcontentsoffile:plistpath];        Self.pickerdata = Dict;        Province name self.pickerprovincesdata = [Self.pickerdata AllKeys];    The default is to take out the data for all municipalities in the first province nsstring *seletedprovince = [Self.pickerprovincesdata objectatindex:0];   Objectforkey: Find the data of the index self.pickercitiesdata = [Self.pickerdata objectforkey:seletedprovince];  
 Self.pickView.data Source = self; 
    Self.pickView.delegate = self;}    -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.}    -(Ibaction) onclick: (ID) Sender {//Set the selected row index starting from 0 nsinteger row1 = [Self.pickerview selectedrowincomponent:0];    Nsinteger row2 = [Self.pickerview selectedrowincomponent:1];    NSString *selected1 = [Self.pickerprovincesdata Objectatindex:row1];        NSString *selected2 = [Self.pickercitiesdata objectatindex:row2];    NSString *title = [[NSString alloc] initwithformat:@ "%@,%@ City", Selected1, Selected2]; Self.label.text = title;} #pragma mark Implementation Protocol Uipickerviewdatasource Method-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) pickerview{// Returns the number of dials in the selector return 2;}  -(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (nsinteger) component{//Returns the number of rows in a selected dial if (component = = 0)    {//Returns the number of provinces return [Self.pickerprovincesdata Count]; } else {//returns the number of cities return [Self.pickercitiesdata Count]; }} #pragma mark implements the protocol Uipickerviewdelegate method//provides display data for a row of a dial in the selector-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (Nsinteger) Row forcomponent: (Nsinteger) component{if (component = = 0) {//Select the province's name return [self    . Pickerprovincesdata Objectatindex:row];    } else {return [self.pickercitiesdata Objectatindex:row]; }}//when a row in a dial is selected, call-(void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (Nsinteger        ) component{if (component = = 0) {nsstring *seletedprovince = [Self.pickerprovincesdata objectatindex:row];        Nsarray *array = [Self.pickerdata objectforkey:seletedprovince];        Self.pickercitiesdata = array;    Refresh [Self.pickerview reloadcomponent:1]; }} @end


The implementation object of the entrusted and the data source Viewcontroller assigned to the Uipickerview attribute delegate and the data source attribute datasource;

This part can be implemented in Interfacebuilder. The assignment will generate itself voluntarily

-(void) viewdidload

{

...


Self.pickerView.dataSource = self;

Self.pickerView.delegate = self;

}

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Common options &lt; Data source protocols, delegation agreements &gt; (iOS development)

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.