1 Feb 2017

Simple Picker View Code...


Simple Picker View Code...




#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>

@property (weak, nonatomic) IBOutlet UITextField *employeeName;

@property (weak, nonatomic) IBOutlet UITextField *employeeDOB;
@property (weak, nonatomic) IBOutlet UITextField *employeeSalary;
@property (weak, nonatomic) IBOutlet UIPickerView *employeeStatusPicker;

@property (weak, nonatomic) IBOutlet UILabel *statusLabel;

@end


#import "ViewController.h"

@interface ViewController ()
{
    NSArray *_pickerData;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    // Initialize Data
    _pickerData = @[@"Active", @"InActive",@"New Entry"];
    
    // Connect data
    self.employeeStatusPicker.dataSource = self;
    self.employeeStatusPicker.delegate = self;
}

// The number of columns of data
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// The number of rows of data
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _pickerData.count;
}

//// The data to return for the row and component (column) that's being passed in
//- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
//{
//    return _pickerData[row];
//}

// Catpure the picker view selection
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    // This method is triggered whenever the user makes a change to the picker selection.
    // The parameter named row and component represents what was selected.
    
    _statusLabel.text = _pickerData[row];
}

- (UIView *)pickerView:(UIPickerView *)pickerView

            viewForRow:(NSInteger)row

          forComponent:(NSInteger)component

           reusingView:(UIView *)view {
    
    UILabel *pickerLabel = (UILabel *)view;
    
    if (pickerLabel == nil) {
        //label size
        CGRect frame = CGRectMake(0.0, 0.0, 70, 30);
        
        pickerLabel = [[UILabel alloc] initWithFrame:frame];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        //here you can play with fonts
        [pickerLabel setFont:[UIFont fontWithName:@"Times New Roman" size:14.0]];
        
        [pickerLabel setTextColor:[UIColor redColor]];
        
    }
    //picker view array is the datasource
    [pickerLabel setText:[_pickerData objectAtIndex:row]];
    
    return pickerLabel;

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end


No comments:

Post a Comment

Recent Posts

Codable demo

Link: https://www.dropbox.com/s/kw7c1kgv1628bh7/codableDemo.zip?dl=0