IOS:另一个控制器中的调用方法

时间:2013-04-09 14:49:07

标签: iphone ios uiviewcontroller

我有两个独立的类FirstController和SecondController,用故事板创建。问题是我想在SecondController.m,FROM FirstController中调用方法。对于前者:

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
 }

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

更新 我想使用'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参数?

3 个答案:

答案 0 :(得分:1)

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

通过以下链接查看我的答案。

Can't use reloadData from another class

这两个选项都可用。

答案 1 :(得分: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];

答案 2 :(得分:-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];
 }

只有在不使用ARC时才需要-release来电。