1

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"];



}
Christien
  • 1,213
  • 4
  • 18
  • 32
  • You can use Local notification for this. – spider1983 Dec 14 '12 at 11:24
  • can u plz explain more...what i did...I made`string` `nonatomic,retain` of picker and in view controller id did....`@property (nonatomic, retain) Picker1 *delegate1; `...then `nslog(@"value %@",delegate1.string);` in button press action...but nothing displays when i click button...means value is null – Christien Dec 14 '12 at 11:29
  • @spider1983 actually I have to select these values and then by taking those values in ViewController,,some action has to perform..but unable to take values – Christien Dec 14 '12 at 11:36
  • use this line [yourPickerView selectRow:1 inComponent:0 animated:NO]; – Sudha Tiwari Dec 14 '12 at 11:38
  • @Christien where you create your pickerview.. – Sudha Tiwari Dec 14 '12 at 11:43
  • @Christien Did you make your pickerview programmatically?? Or use in Xib? – Sudha Tiwari Dec 14 '12 at 11:48

2 Answers2

1

You can use delegates

in picker.m,above @interface section do this

@protocol pickerDelegate <NSObject>

-(void)didFinishPicking:(NSString *)pickedStr;

@end

make property of this protocol

@property(nonatomic,weak)id<pickerDelegate>delegate

and in - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component method, do this

string=[NSString stringWithFormat:@"%@",[list objectAtIndex:row]];



[self.delegate didFinsihPicking:string];

now in viewcontroller.h, in the method

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath


 Picker1 *pick= [[Picker1 alloc] init];

[pick setDelegate : self];

and implement the dlegate method in viewcontroler.h

    -(void)didFinishPicking:(NSString *)pickedStr
{
[self setStr:pickedStr]
}

where str is the string variable you need to declare in viewcontroller.h and print this string on button click event

Vishal Singh
  • 4,400
  • 4
  • 27
  • 43
  • one more thing you have to make your viewcontroller conform to pickerDelegate in interface – Vishal Singh Dec 14 '12 at 11:50
  • ok if I need to need to give condition in button like if pickers value from picker ==A ,open url...how to do this...like I m doin in button{`If(piker1.string == @"raj") { [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://www.google.com"]];}` – Christien Dec 14 '12 at 12:02
  • if([picker1.string isEqualToString:@"raj"]) – Vishal Singh Dec 14 '12 at 12:11
  • ya fyn i used this with `[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://www.google.com"]]` to check...but its not opening url – Christien Dec 14 '12 at 12:19
  • try opening it in a webView, – Vishal Singh Dec 14 '12 at 12:28
  • hey!! what,if i use picker delegate in other page...which don't have uitableview... didSelectRowAtIndexPath we can use – Christien Dec 15 '12 at 06:37
  • what other page?whichever class you want to trsfer the value from picker, just implement the delegate method didFinishPicking in that class , set that calss as delecate of picker class and thats it..you ont have to change anything in picker class now. – Vishal Singh Dec 15 '12 at 06:51
  • [`set that class as delecate of picker class`]??? whats dis...suppose I am doing this in other class(not with tableview)...I made the same changes ...and above `didSelectRowAtIndexPath` code..is used in `ViewDidload` – Christien Dec 15 '12 at 06:55
  • you please elaborate a little what actually you are trying to do so that i can help – Vishal Singh Dec 15 '12 at 06:57
  • ya thats wat I am asking..I tried the same thing u said in ClassA but not working this time – Christien Dec 15 '12 at 06:59
  • but you have to tell me that how you and where you want to navigate into class A? – Vishal Singh Dec 15 '12 at 07:01
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21174/discussion-between-ivishal-and-christien) – Vishal Singh Dec 15 '12 at 07:01
  • like I have created a simple subclass(view controller) with just I have button in this...when I click on button..It should display in nslog... – Christien Dec 15 '12 at 07:04
  • what i did `-(void)didFinishPicking:(NSString *)pickedStr` and `didSelectRowAtIndexPath` function I defined in viewdidload...then not working.. – Christien Dec 15 '12 at 07:05
  • use the above link to explain me your problem. – Vishal Singh Dec 15 '12 at 07:08
1

It sounds like you need a delegate for your picker to pass the selected value back to the table view. Define the delegate method at the top of your Picker.h file:

@class Picker;

@protocol PickerDelegate <NSObject>

- (void)picker:(Picker *)thePicker didPickValue:(NSString *)theValue;

@end

Also in your Picker.h file, create a weak connection to the delegate which will be the table view controller:

@property (nonatomic, weak) id <PickerDelegate> delegate;

In your table view controller add the delegate to the header file:

@interface ViewController : UITableViewController <PickerDelegate>

You call the delegate method from your Picker.m and implement it in your ViewController.m:

[self.delegate picker:self didPickValue:[NSString stringWithFormat:@"%@",[list objectAtIndex:row]]];

IMPORTANT: When you create the picker, you must tell it who the delegate is going to be. You are doing this within your table view, so you call:

pick.delegate = self;
beev
  • 1,197
  • 2
  • 16
  • 33
  • thanks.. what if i need to give condition in button of Viewcontroller to check first value selected and give action like ..`If(piker1.string == @"A") { [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://www.google.com"]];}` – Christien Dec 14 '12 at 12:06
  • You implement this delegate method in your ViewController.m so you have - (void)picker:(Picker *)thePicker didPickValue:(NSString *)theValue { // call your code here! } – beev Dec 14 '12 at 12:11
  • When the picker calls the delegate method, it is implemented in your ViewController.m. From there, you can do whatever you want with the value(s) passed in by your picker. – beev Dec 14 '12 at 12:12
  • what I did in view controller..@property (nonatomic, retain) Picker1 *delegate1;...then slog of button-> `nslog(@"value",delegate1.string)` its fine ./..giving correct output – Christien Dec 14 '12 at 12:23
  • when if give condition with picker value....`if([picker1.string isEqualToString:@"raj"]{ [UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://www.google.com"]]; }` to check values its not opening – Christien Dec 14 '12 at 12:25
  • You have used NSLog(@"string: %@"); to check the value of the string is correct? How about putting an NSLog after if([picker1.string isEqualToString:@"raj"]{ and before [UIApplication sharedApplication] to check that 'if' is evaluating to TRUE? – beev Dec 14 '12 at 12:28
  • ya thats not working then...even I select the correct one...:( – Christien Dec 14 '12 at 12:44
  • Actually I used the ivishal answer...ur answer giving me error with `list undefined` from `[self.delegate picker:self didPickValue:[NSString stringWithFormat:@"%@",[list objectAtIndex:row]]];` this i used in didSelectRowAtIndexPath – Christien Dec 14 '12 at 12:46
  • So you are successfully getting your string back to your view controller from the picker? – beev Dec 14 '12 at 12:47