简体   繁体   中英

Adding UITextView as subview in UIView then to 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM