0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Not viewDidLoad, loadView. Calling viewDidLoad is sometimes nessecary, but not loadView (AFAIK). Also, did you set the contentView size of your scrollview to be: (numberOfContentViews*contentViewWidth)?
Thanks. Calling viewDidLoad is temporary until I get the textview working. Otherwise, I'll have to create non iBOutlet properties and then assign during viewDidLoad. The textview still doesn't show after the first view with you code. Label still displays fine. I can also add a UIWebView and UIScrollView with a label in it. They show on all pages.
Yes to the (numberOfContentViews*contentViewWidth) question. The content views are displaying fine. I can place controls in each corner and see them. I also tried putting a textview in the middle but it disappears after the first view.
You might try adding the textView Programatically in viewDidLoad to see if you have the same problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.