[Basic iOS control and basic ios Control

Source: Internet
Author: User

[Basic iOS control and basic ios Control
A. Requirements1. use PickerView to make a three-column meal (fruit, main course, drink) with Demo2. the selected meal is displayed in the "display area" in real time. the "random" button is provided to randomly select dishesB. Implementation steps1. drag a PickerView 1 // follow UIPickerViewDataSource, UIPickerViewDelegate 2 @ interface ViewController () <UIPickerViewDataSource, UIPickerViewDelegate> 3 4/** selector */5 @ property (weak, nonatomic) IBOutlet UIPickerView * pickerView; 6 7 @ end 8 9 @ implementation ViewController10 11-(void) viewDidLoad {12 [super viewDidLoad]; 13 // Do any additional setup after loading the view, typically from a nib.14 15 // set performance16 self. pickerView. dataSource = self; 17 // set delegage18 self. pickerView. delegate = self; 19}3. Implement proxy Methods

1 # pragma mark-proxy method 2/** Number of columns */3-(NSInteger) numberOfComponentsInPickerView :( UIPickerView *) pickerView {4 return 3; 5} 6 7/** number of lines in the corresponding column */8-(NSInteger) pickerView :( UIPickerView *) pickerView numberOfRowsInComponent :( NSInteger) component {9 return 5; 10} 11 12/** display content of corresponding columns and rows */13-(NSString *) pickerView :( UIPickerView *) pickerView titleForRow :( NSInteger) row forComponent :( NSInteger) component {14 return @ "content"; 15}
1/** load data, delayed loading */2-(NSArray *) foodCategories {3 return self. foodCategories = [NSArray arrayWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "foods. plist "ofType: nil]; 4} 5 6 # pragma mark-dataSource Method 7/** Number of columns */8-(NSInteger) numberOfComponentsInPickerView :( UIPickerView *) pickerView {9 return self. foodCategories. count; 10} 11 12/** number of lines in the corresponding column */13-(NSInteger) pickerView :( UIPickerView *) pickerView numberOfRowsInComponent :( NSInteger) component {14 NSArray * foods = self. foodCategories [component]; 15 return foods. count; 16} 17 18 19 # pragma mark-delegate Method 20/** display content of corresponding columns and rows */21-(NSString *) pickerView :( UIPickerView *) pickerView titleForRow :( NSInteger) row forComponent :( NSInteger) component {22 NSArray * foods = self. foodCategories [component]; 23 return foods [row]; 24}1/** fruit */2 @ property (weak, nonatomic) IBOutlet UILabel * fruitLabel; 3/** main course */4 @ property (weak, nonatomic) IBOutlet UILabel * mainLabel; 5/** beverage */6 @ property (weak, nonatomic) IBOutlet UILabel * drinkLabel;6. Response to selected events
1/** response to select meal event */2-(void) pickerView :( UIPickerView *) pickerView didSelectRow :( NSInteger) row inComponent :( NSInteger) component {3 NSArray * foods = self. foodCategories [component]; 4 NSString * food = foods [row]; 5 6 switch (component) {7 case 0: 8 self. fruitLabel. text = food; 9 break; 10 case self. mainLabel. text = food; 12 break; 13 case 2: 14 self. drinkLabel. text = food; 15 break; 16 default: 17 break; 18}
1/** randomly select Meals */2-(IBAction) onRandomClicked {3 // loop all meal types 4 for (int I = 0; I <self. foodCategories. count; I ++) {5 // The number of rows selected before the meal 6 NSInteger originalRow = [self. pickerView selectedRowInComponent: I]; 7 8 NSInteger row = originalRow; 9 10 // all meals contained in this category 11 NSArray * foods = self. foodCategories [I]; 12 // If a random meal is the same as the previous one, continue the random 13 while (row = originalRow) {14 row = arc4random () % foods. count; 15} 16 17 // notification pickerView change select 18 [self. pickerView selectRow: row inComponent: I animated: YES]; 19 20 // change the display area 21 [self pickerView: self. pickerView didSelectRow: row inComponent: I]; 22} 23}

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.