告诉 UIViewController 子视图中发生的事情

发布于 2024-12-11 07:34:26 字数 179 浏览 0 评论 0原文

我有一个 UIViewController ,其中有一个 UIView 作为子视图。我希望 UIViewController 能够收到(或监听)UIView 中某些操作的通知。 执行此操作的标准方法是什么?

I have a UIViewController with a UIView as a subview. I'd like the UIViewController to be notified of (or listen for) certain actions in the UIView. What's the standard way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸩远一方 2024-12-18 07:34:26

如果您只想有一个操作处理程序:

- (void)viewDidLoad {
    [super viewDidLoad];
    // ......
    UIButton *myButton = [[UIButton alloc] init]; // or initWithSOMETHING
    [myButton addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:myButton];
    [myButton release];
    // ......
}

- (void)someMethod:(id)sender {
    if (![sender isKindOfClass:[UIButton class]]) return;
    // Do something about the action here...
}

有关详细信息,请参阅 UIControl 文档

准备和发送操作消息

 – sendAction:to:forEvent:
– sendActionsForControlEvents:
– addTarget:action:forControlEvents:
– 删除目标:操作:forControlEvents:
– actionsForTarget:forControlEvent:
– 所有目标
– 所有控制事件

或者,如果您正在谈论调用父视图控制器的视图控制器,您可以使用 UIViewController 访问“父”视图 控制器:

UIViewController *parentVC = [self presentingViewController];
[parentVC setSomething:self.withSomething];

presentingViewController - 呈现此视图控制器的视图控制器。 (只读)

@property(非原子,只读)UIViewController
*presentingViewController

讨论
该属性的默认实现会向上移动视图
层次结构,从该视图控制器开始。第一个视图
控制器发现收到了
presentViewController:animated:completion: 方法,或者有它的
设置为 YESdefinesPresentationContext 属性作为
财产的价值。它会一直沿着层次结构向上移动,直到
找到一个要返回的值或者它到达根视图控制器。


或者 UIView

[self.view.superview doSomething];

superview - 接收者的超级视图,如果没有,则为零。 (只读)
@property(nonatomic, readonly) UIView *superview

If you just want to have an action handler:

- (void)viewDidLoad {
    [super viewDidLoad];
    // ......
    UIButton *myButton = [[UIButton alloc] init]; // or initWithSOMETHING
    [myButton addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:myButton];
    [myButton release];
    // ......
}

- (void)someMethod:(id)sender {
    if (![sender isKindOfClass:[UIButton class]]) return;
    // Do something about the action here...
}

For more info, see the UIControl Documentation:

Preparing and Sending Action Messages

– sendAction:to:forEvent:
– sendActionsForControlEvents:
– addTarget:action:forControlEvents:
– removeTarget:action:forControlEvents:
– actionsForTarget:forControlEvent:
– allTargets
– allControlEvents

Or, if your talking about a view controller calling a parent view controller, you could use the presentingViewController property of a UIViewController to access the "parent" view controller:

UIViewController *parentVC = [self presentingViewController];
[parentVC setSomething:self.withSomething];

presentingViewController - The view controller that presented this view controller. (read-only)

@property(nonatomic, readonly) UIViewController
*presentingViewController

Discussion
The default implementation of this property walks up the view
hierarchy, starting from this view controller. The first view
controller it finds that received the
presentViewController:animated:completion: method, or that has its
definesPresentationContext property set to YES is returned as the
value of the property. It keeps walking up the hierarchy until it
finds a value to return or it gets to the root view controller.


Or the superview property of a UIView:

[self.view.superview doSomething];

superview - The receiver’s superview, or nil if it has none. (read-only)
@property(nonatomic, readonly) UIView *superview

无人问我粥可暖 2024-12-18 07:34:26

根据您对上面评论的回答,这是一个按钮按下,“标准”方法是使用 UIControl 中的机制。因此,以编程方式,如果您

[myButton addTarget:myViewController action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

按“myButton”,将导致“buttonPressed:”方法在 myViewController 上执行。您还可以使用 Interface Builder 来建立连接。

这段代码可以放在许多不同的地方(在 UIView 中,在 UIViewController 中),因此,变量的确切名称会改变,但是,对于处理按下的按钮,这可能是最简单的方法。

Based on your answer to the comment above, that this is a button press, the "standard" way would be to use the mechanisms in UIControl. So, programmatically, if you do

[myButton addTarget:myViewController action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

Pressing "myButton" will cause the "buttonPressed:" method to be executed on myViewController. You can also use Interface Builder to make the connections.

This code could be put in a number of different places (in the UIView, in the UIViewController) and, as a result, the exact name of the variables would change, but, for handling a button pressed, this is probably the easiest way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文