从子 UIView 访问父视图

发布于 2025-01-02 15:24:42 字数 379 浏览 2 评论 0原文

我有一个带有 xib 的 UIViewController 并使用 Interface Builder 添加了一个子 UIView。 在子 UIView 中,当我单击该视图中的对象时,我希望能够更改整个窗口的标题。

现在我通常会

self.title = @"hi";

在父 UIViewController 上进行该设置。但是有什么方法可以从孩子内部访问父头衔吗?

我已经尝试过

self.superview.title = @"i";
self.parentViewController.title = @"hi";

,但都不起作用。 任何帮助

非常感谢

I have a UIViewController with an xib and using Interface Builder I've added a child UIView.
Within the child UIView, when I click on an object within that view, I want to be able to alter the title of the whole window.

Now I'd normally do that setting

self.title = @"hi";

on the parent UIViewController. But is there any way I can access the parent title from within the child?

I've tried

self.superview.title = @"i";
self.parentViewController.title = @"hi";

but neither work.
Any help much appreciated

thanks

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

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

发布评论

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

评论(1

汹涌人海 2025-01-09 15:24:42

self.superview.title = @"i"; 计算结果为 UIView 类型的对象,并且 UIView 没有 title属性。 UIViewControllerparentViewController 属性,但 UIView 没有。

因此,根本问题是您没有正确分离控制器和视图类。您通常要做的就是让您想要捕获 UIControl 子类上的点击的视图(像 UIButton 这样的东西已经存在,但如果它是自定义的 UIView 子类,那么您可以将其更改为 UIControl 子类,因为 UIControl 本身就是 UIView 的子类),然后在您的控制器添加类似的内容:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // we'll want to know if the view we care about is tapped;
    // we've probably set up an IBOutlet to it but any way of
    // getting to it is fine
    [interestingView
               addTarget:self
               action:@selector(viewTapped:)
               forControlEvents:UIControlEventTouchDown];

     // UIButtons use UIControlEventTouchUpInside rather than
     // touch down if wired up in the interface builder. Pick
     // one based on the sort of interaction you want
}

// so now this is exactly like an IBAction
- (void)viewTapped:(id)sender
{
    self.title = @"My new title";
}

所以你明确地不要向视图投入任何有关其在视图层次结构中的位置或视图控制器打算如何操作的知识。您只需告诉它在收到用户交互时向您大声喊叫即可。

self.superview.title = @"i"; evaluates to an object of type UIView, and UIView has no title property. UIViewControllers have a parentViewController property but UIViews don't.

So the fundamental problem is that you're not properly separating your controller and your view classes. What you'd normally do is make the view you want to catch taps on a subclass of UIControl (which things like UIButton already are, but if it's a custom UIView subclass then you can just change it into a UIControl subclass since UIControl is itself a subclass of UIView), then in your controller add something like:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // we'll want to know if the view we care about is tapped;
    // we've probably set up an IBOutlet to it but any way of
    // getting to it is fine
    [interestingView
               addTarget:self
               action:@selector(viewTapped:)
               forControlEvents:UIControlEventTouchDown];

     // UIButtons use UIControlEventTouchUpInside rather than
     // touch down if wired up in the interface builder. Pick
     // one based on the sort of interaction you want
}

// so now this is exactly like an IBAction
- (void)viewTapped:(id)sender
{
    self.title = @"My new title";
}

So you explicitly don't invest the view with any knowledge about its position within the view hierarchy or how your view controllers intend to act. You just tell it to give you a shout out if it receives a user interaction.

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