将 UITextView 添加为 UIView 中的子视图,然后添加到 UIScrollView

发布于 2024-07-21 06:02:59 字数 1003 浏览 0 评论 0原文

我有一个 UIViewController (ContentViewController) 连接到 View xib。 我将 TextView 放在 UIView 上,它在 IB 的层次结构中显示为子视图。
ContentViewController 有一个标签作为出口,它连接到 ContentView.xib 上的标签。 当下面的代码执行时,第一个视图显示文本视图,但滚动其他视图/页面仅显示标签。

for(int i=0; i< 5; i++){
  ContentViewController *contentView = [[ContentViewController alloc] initWithNibName:@"ContentView" bundle:[NSBundle mainBundle]]; 
  [contentView loadView];
  contentView.theLabel.text =[NSString stringWithFormat:@"hello again..%d", i];
  [self.scrollView addSubview:contentView.view];
}

我可以将按钮或文本字段拖到 IB 中的视图上,当我滚动页面时它们都在那里。 上述文字也设置在标签上。 文本视图在第一页后消失。 我也无法设置其文本。 我将一个新的文本视图拖到 UIView 上,它也在第一页之后消失。 我需要为文本视图做一些不同的事情吗?


我发现 UIScrollView 位于每个页面上。 如果我在滚动视图所在的位置垂直滑动,则会出现文本。 但我必须对第一个滚动视图之后的所有滚动视图执行此操作。

但是,一旦我滑动它们,文本就会保持可见,直到应用程序重新启动。 如果没有足够的文本来导致滚动,则文本视图将永远不会出现。 对这种行为有什么建议吗?

I have a UIViewController (ContentViewController) wired to a View xib. I drop a TextView on the UIView and it appears as a subview in the hierarchy in IB.
ContentViewController has a label as an outlet, which is wired to a label on ContentView.xib. When the code below executes, the first view shows the text view but scrolling through the other views/pages reveals only the label.

for(int i=0; i< 5; i++){
  ContentViewController *contentView = [[ContentViewController alloc] initWithNibName:@"ContentView" bundle:[NSBundle mainBundle]]; 
  [contentView loadView];
  contentView.theLabel.text =[NSString stringWithFormat:@"hello again..%d", i];
  [self.scrollView addSubview:contentView.view];
}

I can drag a button or textfield onto the view in IB and they are all there as I scroll through pages. The above text is set on the label as well. The textview disappears after the first page. I also can't set its text. I've dragged a new textview onto the UIView and it disappears after the first page as well. Is there something different I need to do for the textview?


I've found that the UIScrollView is on each page. If I swipe vertically where the scrollview is, text will appear. But I have to do that for all scrollviews after the first one.

However, once I swipe them, the text remains visible until the app is restarted. If there isn't enough text to cause scrolling, the textview will never appear. Any suggestions on this behavior?

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

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

发布评论

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

评论(1

差↓一点笑了 2024-07-28 06:02:59

尝试:

[textView setNeedsDisplay];


原始答案

您不必以不同的方式处理文本视图。

我首先担心你对“loadview”的调用。 你永远不应该直接调用它。 当需要时它会被自动调用。

另外,您应该在循环之前声明 contentview 并且会泄漏内存。

您的另一个问题是您在循环期间永远不会更改每个视图的框架。 它们的原点都是{0,0}。 据我从你的代码中可以看出。 这可能就是您的观点被隐藏的原因。

最后,在风格说明上,我不会称我为视图控制器 XXXXXview,它们应该有控制器这个词。

    CGRect frame;
    ContentViewController *myContentViewController = nil;

    for(int i=0; i< 5; i++){
      myContentViewController = [[myContentViewController alloc] initWithNibName:@"ContentView" bundle:[NSBundle mainBundle]];

      frame = myContentViewController.view.frame;
      frame.origin.x=(frame.size.width*i);
      myContentViewController.view.frame=frame; 

      myContentViewController.theLabel.text =[NSString stringWithFormat:@"hello again..%d", i];
      [self.scrollView addSubview: myContentViewController.view];
      [myContentViewController release];
    }

加上:
另请检查滚动视图 contentView 框架属性:

scrollView.contentSize = CGSizeMake(numberOfContentViews*contentViewWidth, contentViewHeight);

要添加更多:

尝试在 viewDidload 或 viewDidAppear 中以编程方式添加 textView 以查看是否有效。

在您发表评论之后,如果我不得不进行另一个猜测,我会说您可能在添加 textView 后在代码中的其他位置对其进行操作。 开始注释掉任何影响 textView 的行,看看会发生什么。

Try :

[textView setNeedsDisplay];


Original Answer

You wouldn't have to deal with the textview differently.

I first worry about your calling of "loadview". You are never supposed to call that directly. It is invoked automtically when needed.

Also, you should declare contentview before the loop and you are leaking memory.

Your other problem is you never change the frame of each view during the loop. They all have an origin of {0,0}. As far as I can see from your code. This may be why your views are hidden.

Lastly, on a style note, I wouldn't call me view controllers XXXXXview, they should have the word controller in them.

    CGRect frame;
    ContentViewController *myContentViewController = nil;

    for(int i=0; i< 5; i++){
      myContentViewController = [[myContentViewController alloc] initWithNibName:@"ContentView" bundle:[NSBundle mainBundle]];

      frame = myContentViewController.view.frame;
      frame.origin.x=(frame.size.width*i);
      myContentViewController.view.frame=frame; 

      myContentViewController.theLabel.text =[NSString stringWithFormat:@"hello again..%d", i];
      [self.scrollView addSubview: myContentViewController.view];
      [myContentViewController release];
    }

To Add:
Also check the scrollviews contentView frame property:

scrollView.contentSize = CGSizeMake(numberOfContentViews*contentViewWidth, contentViewHeight);

To add more:

Try adding the textView programatically in viewDidload or viewDidAppear to see if that works.

After your comments, if I had to take another guess, I would say that you might be manipulating the textView somewhere else in your code after it is added. Start commenting out any lines that affect the textView and see what happens.

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