1

I have two separated classes FirstController and SecondController, created with storyboards. The problem is that I want to call method in SecondController.m, FROM FirstController. For ex. :

SecondController.m

-(void)myMethod:(CGPoint)pt {...} // It's important that there is a paramterer

FirstController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // Call myMethod
 }

How to do this in the easiest way?

Update: I want to use notification from 'aBilal17' link:

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"updateLeftTable"
                                              object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(checkRes:)     name:@"updateLeftTable" object:nil];

(..)

-(void)checkRes:(NSNotification *)notification
{
   if ([[notification name] isEqualToString:@"updateLeftTable"])
   {
      [myMethod ?
    }
}

In other class:

[[NSNotificationCenter defaultCenter] postNotificationName:@"updateLeftTable" object:self];

But now, how to pass my CGPoint argument using this?

user2262230
  • 199
  • 1
  • 3
  • 17
  • What have you looked at? There are hundreds of questions regarding this sort of thing on stackoverflow. – Popeye Apr 09 '13 at 14:53
  • Unless you're talking about delegate protocols or notifications, you may wish to introduce more model in your MVC. You generally shouldn't be calling code from other controllers. – Marcus Adams Apr 09 '13 at 15:30

3 Answers3

1

You can use NSNotification or custom delegate for it.

check my ans on the following link.

Can't use reloadData from another class

Both options are available on it.

Community
  • 1
  • 1
aBilal17
  • 2,974
  • 2
  • 17
  • 23
  • Thanks! Notifications seems to be the best solution. But how to pass an argument (CGPoint) using it (Let's say that I have exactly the same notification as in your link at this moment)? – user2262230 Apr 09 '13 at 16:29
0

To pass CGPoint using NSNotificationCenter, you'll need to use NSValue:

NSValue *pointValue = [NSValue valueWithCGPoint:myPoint];
[[NSNotificationCenter defaultCenter] postNotificationName:@"updateLeftTable" object:pointValue];

In your checkRes: method, you can obtain the point with the object property of the passed NSNotification:

NSValue *pointValue = (NSValue *)[notification object];
CGPoint myPoint = [pointValue CGPointValue];
dalton_c
  • 6,876
  • 1
  • 33
  • 42
-1
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // Call myMethod
    SecondController *controller = [[SecondController alloc] init];
    [controller myMethod:pt];
    [self.navigationController pushViewController:controller animated:YES];
    [controller release];
 }

The -release call is only necessary when you are not using ARC.

cutsoy
  • 10,127
  • 4
  • 40
  • 57
  • This is wrong. The OP states that he created both controllers in the storyboard. Using alloc init will create a new instance rather than getting the one in the storyboard. – rdelmar Apr 09 '13 at 15:57