我有两个独立的类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参数?
答案 0 :(得分:1)
答案 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
来电。