PickerView Controller



                      PickerView Controller 

i) There is no need to change Main.m,  AppDelgate.h.

AppDelegate.m


#import "AppDelegate.h"
#import "ViewController.h"
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    UINavigationController *nav=[[UINavigationController alloc]init]; 
    self.viewController.title=@"root";
    self.window.rootViewController = nav;
    [nav setTitle:@"pick item"];
    [nav addChildViewController:self.viewController];
    [self.window makeKeyAndVisible];
    return YES;
    
}

ViewController.h


#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>{   
    IBOutlet UIPickerView *speedPicker;
    IBOutlet UILabel *lbl,*lbl1,*lbl2,*lbl3;
}
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) NSString *strName,*strName1,*strName2,*strName3;
@end


ViewController.m


#import "ViewController.h"
@implementation ViewController
@synthesize arr;
@synthesize strName,strName1,strName2,strName3;

- (void)viewDidLoad
{
    lbl.text=self.strName;
    lbl1.text=self.strName1;
    lbl2.text=self.strName2;
    lbl3.text=self.strName3;
    
// Do any additional setup after loading the view, typically from a nib.
    self.arr= [NSMutableArray arrayWithObjects:@"ramudu",@"laxman",@"anil",@"nagendra",@"sekhar",@"venki",@"suri", nil];
//    self.arr1= [NSMutableArray arrayWithObjects:@"apple",@"orange",@"banana",@"grapes",@"pine apple",@"promegranate", nil];
    speedPicker=[[UIPickerView alloc]init];
    speedPicker.delegate=self;
    speedPicker.dataSource=self;
    [super viewDidLoad];
}


// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 4;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [arr count];

}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [self.arr objectAtIndex:row];
 }
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    if(component==0)
    lbl.text =  [self.arr objectAtIndex:row];
    else if(component==1)    lbl1.text = [self.arr objectAtIndex:row];
    else if(component==2)    lbl2.text = [self.arr objectAtIndex:row];
    else   lbl3.text = [self.arr objectAtIndex:row];
}

@end




tags: ios code examples, ios concepts, iphone sample codes, iphone sample projects, sample apps for iphone with source code, sample programs in iphone

0 comments:

Post a Comment

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Blogger Templates