UIPickerView simple application: uipickerview Application

Source: Internet
Author: User

UIPickerView simple application: uipickerview Application

Below are some

 

 

 

 

 

The following is the code. It is boring, but it is not difficult.

 

 

#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>@property(strong,nonatomic) UIPickerView *picker;@property(strong,nonatomic) NSMutableArray *sheng;@property(strong,nonatomic) NSMutableArray *shi;@property(strong,nonatomic) NSArray *array;@end

 

 

# Import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; self. sheng = [NSMutableArray array]; self. shi = [NSMutableArray array]; NSBundle * bundle = [NSBundle mainBundle]; NSString * path = [bundle pathForResource: @ "city. plist "ofType: nil]; self. array = [NSArray arrayWithContentsOfFile: path]; for (NSDictionary * ar1 in self. array) {NSArr Ay * ar2 = [ar1 objectForKey: @ "Cities"]; [self. sheng addObject: [ar1 objectForKey: @ "State"]; for (NSDictionary * ar3 in ar2) {// NSLog (@ "% @", [ar3 objectForKey: @ "city"]); [self. shi addObject: [ar3 objectForKey: @ "city"] ;}} self. picker = [[UIPickerView alloc] initWithFrame: CGRectMake (0,200,414,300)]; self. picker. backgroundColor = [UIColor greenColor]; [self. view addSubview: self. picker]; self. picker. dele Gate = self; self. picker. dataSource = self ;}# pragma mark-Data Source preview:-(NSInteger) numberOfComponentsInPickerView :( UIPickerView *) pickerView {return 2;} # pragma mark-Data Source pickerView: attributedTitleForRow: forComponent: -(NSInteger) pickerView :( UIPickerView *) pickerView numberOfRowsInComponent :( NSInteger) component {if (component = 0) {return self. sheng. count;} return self. shi. cou Nt ;}# pragma mark-display information method delegate-(NSString *) pickerView :( UIPickerView *) pickerView titleForRow :( NSInteger) row forComponent :( NSInteger) component {if (component = 0) {return self. sheng [row];} return self. shi [row] ;}# pragma mark-information of the selected row-(void) pickerView :( UIPickerView *) pickerView didSelectRow :( NSInteger) row inComponent :( NSInteger) component {if (component = 0) {// clear the selected City [self. shi removeAllObjects ]; NSDictionary * dic = [self. array objectAtIndex: row]; // NSLog (@ "% @", dic); NSArray * arr1 = dic [@ "Cities"]; NSMutableArray * arr2 = [NSMutableArray array]; for (NSDictionary * dic in arr1) {[arr2 addObject: dic [@ "city"];} self. shi = arr2; [self. picker selectRow: row inComponent: 0 animated: YES]; [self. picker reloadComponent: 1];} else {NSInteger firstRow = [self. picker selectedRowInComponent: 0]; NSInteger SecondRow = [self. picker selectedRowInComponent: 1]; NSString * firstString = [self. sheng objectAtIndex: firstRow]; NSString * secondString = [self. shi objectAtIndex: secondRow]; NSString * message = [NSString stringWithFormat: @ "are you sure you want to select %? ", FirstString, secondString]; UIAlertController * alertMessage = [UIAlertController failed: @" prompt "message: message preferredStyle: Success]; UIAlertAction * cancle = [UIAlertAction actionWithTitle: @ "cancel" style: incluhandler: nil]; UIAlertAction * OK = [UIAlertAction actionWithTitle: @ "OK" style: UIAlertActionStyleDefault handler: nil]; [alertMessage addAction: cancle]; [alertMessage addAction: OK]; [self presentViewController: alertMessage animated: YES completion: nil]; NSLog (@ "% @", firstString, secondString );}} # pragma mark-Row Height-(CGFloat) pickerView :( UIPickerView *) pickerView rowHeightForComponent :( NSInteger) component {if (component = 0) {return 80;} return 50 ;} -(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end

 

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.