presentation5 picker view
TRANSCRIPT
iOS ProgrammingObjective C + iOS
Profile
Ankit Desai
Ph.D. Scholar, IET, Ahmedabad University
Education: M. Tech. CE, B.E. I. T.
Experience: 7.5 Years (Academic and Research)
Research Interest: IoT, Big Data Analytics,
Machine Learning, Data Mining.
UIPickerView Control
Drag a UIPickerView on xib
Add delegates after class-name
<UIPickerViewDataSource,UIPickerViewDelegate>
Declare two NSArray in .h file
NSArray *arrItems;
NSArray *arrLocation;
Initialize arrays in .m files- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
arrItems=[[NSArrayalloc]initWithObjects:@"iphone",@"android",@"java",@"testing", nil];
arrLocation=[[NSArrayalloc]initWithObjects:@"ahmedabad",@"baroda",@"surat", nil];
}
Add delegates methods 1 & 2 // returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{ return 2; }
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
if (component==0) {
return [arrItems count];
}
else {
return [arrLocation count];
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
if (component==0) {
return [arrItems objectAtIndex:row];
}
else {
return [arrLocation objectAtIndex:row];
}
}
Add delegates methods 3
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
NSString *Str;
if (component==0) {
Str =[arrItems objectAtIndex:row];
}
else{
Str = [arrLocation objectAtIndex:row];
}
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"selected" message:Str
delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[alert show];
}
Add delegates methods 4
Output
Thank you
For further communication or queries:
Contact me:
www.fb.com/desaiankitb