1

我有两个独立的类 FirstController 和 SecondController,用故事板创建。问题是我想从 FirstController 调用 SecondController.m 中的方法。例如。:

第二控制器.m

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

第一控制器.m

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

如何以最简单的方式做到这一点?

更新: 我想使用来自“aBilal17”链接的通知:

[[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 ?
    }
}

在其他班级:

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

但是现在,如何使用这个来传递我的 CGPoint 参数?

4

3 回答 3

1

您可以使用 NSNotification 或自定义委托。

在以下链接上检查我的答案。

不能使用其他类的 reloadData

这两个选项都可用。

于 2013-04-09T14:55:01.983 回答
0

要使用 NSNotificationCenter 传递 CGPoint,您需要使用 NSValue:

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

在您的checkRes:方法中,您可以使用传递的 NSNotification 的 object 属性获取点:

NSValue *pointValue = (NSValue *)[notification object];
CGPoint myPoint = [pointValue CGPointValue];
于 2013-04-09T17:34:02.393 回答
-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];
 }

仅当-release您不使用 ARC 时才需要调用。

于 2013-04-09T14:53:21.360 回答