UITextView:文本仅在我手动移动后才出现

发布于 2024-12-07 20:49:42 字数 2041 浏览 0 评论 0原文

我有一个非常好奇的案例(类似于 这个):

我有一个简单的 UITextView,它位于我以编程方式加载(NIB)的 UIView 中。内容从 TXT 文件加载到 UITextView.text 中。这发生在 viewDidLoad 中。

如果我的文本只有几行并且不需要滚动,那么一切都很好。但是,如果文本比 UITextView 长,则不会显示文本。只有当我触摸屏幕并尝试滚动时,文本才会突然弹出。

我尝试了这个微调以使文本出现在 viewDidLoad 中:

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

但它不起作用。我还确保调用 [super viewWillAppear] 等,但同样,没有运气。唯一有帮助的是,如果我初始化视图并将文本加载到 viewDidAppear 中。但是,除非我滚动,否则 UITextView 的最后一部分仍然不会显示。

有什么想法为什么 UITextView 会这样吗?我不知道,如果有任何建议,我将不胜感激,因为这已经让我好几天了。

编辑:

我现在知道是什么导致了这种奇怪的行为,但我仍然不知道为什么会这样。将显示 PageView 的 ViewController 被动画化到视图中(将 viewController 滑入屏幕的简单动画块)。如果我去掉动画,一切都很好。但我不知道为什么。

这是更多代码(不确定是否有助于澄清)。这是我初始化视图的 viewController。

- (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 ];

}

这是 PageView 类的 init:

    -(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];


}

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];


}

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

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

发布评论

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

评论(1

别再吹冷风 2024-12-14 20:49:42

好的,我通过简单地使用 UINavigationController 来处理发现有问题的 UITextView 的视图来解决这个问题。我仍然不明白为什么这会导致问题,但如果您遇到此问题,则 UIViewController 的结构以及它们刷新(或不刷新)UITextView 的方式可能有问题。

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.

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