Hello,
I'm trying to implement UIPicker as a subview of a current ViewController. When the user taps on a textfield, the keyboard popups instead of the UIPicker. I was pointed to the following tutorial as a guide:
UIPickerView - Creating a simple picker view - iPhone SDK Articles
My code is as follows:
NewWorkoutViewController.h
Code:
@interface NewWorkoutViewController : UIViewController {
IBOutlet UIBarItem *saveButton;
IBOutlet UIBarItem *backButton;
IBOutlet UILabel *time;
//IBOutlet UITextView *routePicker;
IBOutlet UIPickerView *pickerView;
int counterInt;
NSTimer *myTimer;
NSInteger *startInterval;
NSInteger *stopInterval;
NSInteger *elapsedInterval;
}
@property (retain,nonatomic) NSTimer *myTimer;
-(IBAction)backButton;
-(IBAction)saveButton;
-(IBAction)mapButton:(id) sender;
-(IBAction)statisticsButton:(id) sender;
-(IBAction)startTimerButton;
-(IBAction)stopTimerButton;
-(IBAction)routePicker;
-(void)showActivity;
@end
NewWorkoutViewController.m
Code:
#import "NewWorkoutViewController.h"
#import "StatisticsViewController.h"
@implementation NewWorkoutViewController
@synthesize myTimer;
-(void)routePicker
{
PickerViewController *pvController = [[PickerViewController alloc] initWithNibName:@"PickerView" bundle:[NSBundle mainBundle]];
[self presentModalViewController:pvController animated:YES];
}
-(void)applicationDidFinishLaunching:(UIApplication *)application
{
pvController = [[PickerViewController alloc] initWithNibName:@"PickerView" bundle:[NSBundle mainBundle]];
[window addSubview:pvController.view];
// Override point for customization after application launch
[window makeKeyAndVisible];
}
- (void)dealloc {
[myTimer release];
[super dealloc];
}
@end
PickerViewController.h
Code:
#import <UIKit/UIKit.h>
@interface PickerViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>{
IBOutlet UIPickerView *pickerView;
NSMutableArray *list;
}
@end
PickerViewController.m
Code:
#import "PickerViewController.h"
@implementation PickerViewController
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger) forComponent:(NSInteger)component{
return[list count];
}
-(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return[list objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSLog(@"Selected item: %@ index of selected item: %i",[list objectAtIndex:row], row);
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
list = [[NSMutableArray alloc] init];
[list addObject:@"red"];
[list addObject:@"blue"];
[list addObject:@"yellow"];
[list addObject:@"pink"];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[list release];
[super dealloc];
}
At the moment pvController is giving me an error as its not declared, the tutorial has the following code in place:
Code:
@class PickerViewController;
@interface PickerViewAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
PickerViewController *pvController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end
My PickerView is not setup as the AppDelegate, its a ViewController, so I'm not sure what to do ?
Regards, Stephen