开发者

UITextView: Text only appears after I manually moved it

I have a very curious case (similar to this one):

I have a simple UITextView which is located in a UIView which I load programatically (NIB). Contents are loaded from a TXT file into the UITextView.text. This happens in the viewDidLoad.

Everything is fine if my text is only a couple of lines and if there is no need for scrolling. If the text is longer than the UITextView, however, the text will NOT BE DISPLAYED. Only if I touch the screen and try to scroll, will the text suddenly pop up.

I tried this nudge to get the text appear in the viewDidLoad:

textView.contentOffset = CGPointMake(0.0, 1.0);
textView.contentOffset = CGPointMake(0.0, 0.0);

But it won't work. I also made sure to call [super viewWillAppear] etc., but again, no luck. The only thing which helps a bit is if I init my view and load the text in the viewDidAppear. However, the last part of the UITextView is still not shown unless I scroll.

Any ideas why the UITextView is behaving this way? I have no clue and would be grateful for any suggestions as this is holding me up for days now.

EDIT:

I now know what is causing this strange behaviour, but I still don't know why this can be. The ViewController in which the PageView will be displayed is animated into view (simple animation block to slide the viewController into the screen). If I take away the animation, everything is fine. I have no idea why, though.

Here is a bit more code (not sure if it helps to clarify). This is the viewController where I init the view.

- (void)viewDidLoad {

    [super viewDidLoad];

    if (self.currentPageView == nil) {

        NSLog(@"createPage...");
        PageView *cpv = [[PageView alloc] init];
        self.currentPageView = cpv;
        [cpv release];

        [self.view addSubview:currentPageView.view];

    }    

    [self loadContentsFromTXTFile];

    [currentPageView setTitle:title
                         date:date
                         text:text ];

}

This is the init of the PageView class:

    -(id)init {

    //self = [super initWithNibName:nil bundle:nil];
    self = [super initWithFrame:CGRectZero];

    if (self) {

        [[NSBundle mainBundle] loadNibNamed:@"PageView" owner:self options:nil];
        [self addSubview:self.view];    

    }

    return self;

}

-(void)setTitle:(NSString *)title date:(NSString *)date text开发者_如何学编程:(NSString *)text {

    UILabel *mytitle = [self titleOfPage];
    [mytitle setText:title];

    UILabel *mydate = [self dateOfPage];
    [mydate setText:date];

    UITextView *mytext = [self textOfPage];
    [mytext setText:text];


}


Ok, I solved this by simply using a UINavigationController to handle the views in which the problematic UITextView was found. I still don't understand why this has caused problems, but if you experience this problem, there may be something wrong with the structure of your UIViewControllers and the way they refresh (or don't refresh) the UITextView.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜