UIScrollView 上 UIViewController 上 UILabel 的属性

Properties of UILabel on UIViewController on UIScrollView

考虑以下情况: 我有这个 myViewcontroller。在 Storyboard 上,我添加了一个 UIViewController,将其定义为 myViewcontroller,上面有几个 UILabel。我已将它们正确连接到 IBOutlets。

然后,我有一个滚动视图。我将 myViewcontroller 的实例添加到该滚动视图中:

- (void)configureInfoViewController
{
    for (int i = 0; i < [Lists count]; i++) {
        myViewcontroller *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"myLabelLayout"];
        [self addChildViewController:vc];
    }
}

- (void)configureScrollView
{
    for (int i = 0; i < [self.childViewControllers count]; i++) {
        CGRect frame = theScrollView.frame;
        ...
        [[[self.childViewControllers objectAtIndex:i] view] setFrame:frame];
        [theScrollView addSubview:[[self.childViewControllers objectAtIndex:i] view]];
    }
}

因此,滚动视图中的子视图与列表中的对象一样多。到目前为止,一切都很好。 但是现在,我想在其中一个子视图中设置标签的文本。我想通过在 myViewcontroller 的同一个实例上按下 UIButton 来实现这一点。

问题是:如何设置 myLable.title? 我希望这是有道理的。


我认为真正的问题是,困扰我的是: 我怎么知道我的 UIButton 位于哪个子视图上。然后我可以将该元数据发送到其他方法。谢谢

我假设你已经为你的标签创建了出口,当你按下按钮时,你不能访问标签并在按钮的 IBAction 方法中更改它的文本吗?

这是你需要的,

#import "MyViewcontroller.h"

@implementation MyViewcontroller

- (IBAction)myButtonAction:(id)sender {
    _myLabel.text = @"My custom value";
}

@end

仅供参考,您的 InfoViewController 您的 configureScrollView 方法应该是这样的。我没有看到您的代码中调整了滚动视图内容的大小。

- (void)configureScrollView {
    for (int i=0; i<self.childViewControllers.count; i++) {
        CGRect frame = theScrollView.frame;
        // scrolling vertically here
        frame.origin.y = CGRectGetHeight(frame) * i;

        UIView *view = [[self.childViewControllers objectAtIndex:i] view];
        [view setFrame:frame];
        [theScrollView addSubview:view];
    }

    theScrollView.contentSize = CGSizeMake(CGRectGetWidth(theScrollView.frame), CGRectGetHeight(theScrollView.frame) * self.childViewControllers.count);
}