I am new in programming.I have an basic doubt of inheritance.I have a UITableView in ViewController and when I select first row of Tableview.It take me to the Picker page in which only I have to select value from UIPickerViewthats fine now I have to NSLOG selected value of picker in a ViewController in button.this is small problem I am facing.
VIewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
tableview =[[UITableView alloc]initWithFrame:CGRectMake(0,0,([UIScreen mainScreen].bounds.size.width),([UIScreen mainScreen].bounds.size.height/2)) style:UITableViewStyleGrouped];
tableview.delegate=self;
tableview.dataSource=self;
[self.view addSubview:tableview];
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(pressed) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"START " forState:UIControlStateNormal];
button.frame = CGRectMake(62, 250, 196, 37);
[self.view addSubview:button];
thearray= [[NSArray alloc]initWithObjects:@"A",@"B ",@"C",@"D",@"E" ,nil];
[super viewDidUnload];
}
-(void)pressed{
// nslog value..i need here the picker value
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 5;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row==0){
Picker1 *pick= [[Picker1 alloc] init];
[self navigationController] pushViewController:pick animated:YES]
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NS IndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.textLabel.text=[thearray objectAtIndex:indexPath.row];
return cell;
}
Picker.m
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [list count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [list objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
// update label text to show selected option
string=[NSString stringWithFormat:@"%@",[list objectAtIndex:row]];
label.text=string;
[self dismissModalViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
list =[[NSMutableArray alloc]init];
[list addObject:@"A"];
[list addObject:@"B"];
[list addObject:@"C"];
}
You can use delegates
in picker.m,above @interface section do this
-(void)didFinishPicking:(NSString *)pickedStr;
make property of this protocol
and in – (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component method, do this
now in viewcontroller.h, in the method
and implement the dlegate method in viewcontroler.h
where str is the string variable you need to declare in viewcontroller.h and print this string on button click event