1

I'm quite new to this site and iOS development. I have an app that I'm working on, in it, I'm working on a view where when the users taps one button named "zones" the view calls a table view with a list of zones. I'm currently able to make a selection. But what I want is to return back to the previous view when user makes a selection on that table and I want to copy that text in the selection to a variable. Tried so many things. Nothing came close :(

   - (void)viewDidLoad
  {
[super viewDidLoad];


 zones=[NSArray arrayWithObjects:@"392A", @"382F", @"388A", @"373A", @"372A", @"373B", @"366A", @"367A", @"368B", @"362A", @"364A", @"356A",@"357A", @"364B", @"354A",@"353A", @"342A", nil];
NSLog(@"Apple");
// Do any additional setup after loading the view.
}

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell           forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"Home.png"]];
}

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
return [zones count];
}




-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
static NSString *tableIdentifier = @"zonesTable";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];



if (cell==nil)
{
    cell=[[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:tableIdentifier];

}

cell.textLabel.text = [zones objectAtIndex:indexPath.row];

return cell;

}

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

 {

UITableViewCell *cellValue = [tableView cellForRowAtIndexPath:indexPath];

NSLog(@"%@", cellValue); //Not Even getting the NSLog output in Xcode ;(


  } 
Sajin
  • 13
  • 4
  • You need to post code which you have attempted. Such as, the variable which will hold the text, and the tableview code. – ZeMoon May 26 '14 at 06:09
  • I think you need to use protocols. Have a look at this link ---> http://stackoverflow.com/questions/17192287/what-exactly-are-protocols-and-delegates-and-how-are-they-used-in-ios – Maverick May 26 '14 at 06:26

1 Answers1

1

Try this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.currentCityName = [[self.cityArray objectAtIndex:indexPath.row] cityName];
    [self.cityTableView reloadData];
    [self.delegate cityDidGetSelected:[self.cityArray objectAtIndex:indexPath.row]];

    //putting 1/3 of delay
    [self performSelector:@selector(cancelButtonPressed) withObject:nil afterDelay:0.3];
}

CancelButtonPressed Method:

-(void) cancelButtonPressed {
    [self dismissViewControllerAnimated:YES completion:nil];
}

I have been using a delegate method cityDidGetSelected. Implement it in your view controller and pass the zone or city name to it.

CancelButtonPressed will work if you are using presentViewController to show your tableView.

iBug
  • 2,334
  • 3
  • 32
  • 65