ios - 将目标添加到另一个 View Controller 中的按钮

标签 ios uiviewcontroller uibutton

为什么这行不通?我希望新 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/

相关文章:

ios - 为什么 TabBar 在 segue 之后隐藏?

ios - 在滚动幻灯片中的每个 UICollectionViewCell 中弄乱 UIButton

ios - 如何将 UIButton.isSelectable 与 RXSwift 绑定(bind)

ios - AVCapturePhotoOutput : If you specify a non-nil format dictionary in your settings, 您的代表必须响应选择器

ios - 使用 Parse 保存数据,现有项目

ios - 一旦用户切换相机隐私设置,防止 AVCaptureSession 崩溃

iphone - “试图弹出一个不存在的 View Controller 。”

ios - PresentViewController dismiss 不会返回到之前的 viewController

ios - 用户单击自定义按钮时,将其更改

ios - 如何查看用户创建了哪些 Siri 快捷方式