为什么这行不通?我希望新 View Controller 中的 closeBtn 调用当前 View Controller 中名为 dismiss: 的方法。
NewViewController *newVC = [[NewViewController alloc] initWithNibName:@"NewViewController" bundle:[NSBundle mainBundle]];
[newVC.closeBtn addTarget:self action:@selector(dismiss:) forControlEvents:UIControlEventTouchUpInside];
dismiss: 方法从不在当前 View Controller 中调用。 closeBtn 已正确设置为 NewViewController 中的属性并链接到 .xib 文件中。
最佳答案
创建另一个 Controller 的对象并在 addTarget 中指定它。在 Action 参数中给出方法的名称。 即 anotherController *obj; [按钮 addTarget:obj Action :@selector(MethodName) forControlEvents:UIControlEventTouchDown];
关于ios - 将目标添加到另一个 View Controller 中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7828656/