gpt4 book ai didi

ios - 当 View 出现时创建一个 UILabel 并在 View 消失时销毁它

转载 作者:行者123 更新时间:2023-11-28 21:46:27 26 4
gpt4 key购买 nike

对于我的应用程序的教程,我正在尝试创建一个 UILabel,它会在 View 出现时在显示的屏幕上漂移,然后被销毁,这样如果用户在教程期间返回到该 View ,就会创建 UILabel一次又一次地飘过页面。这是我使用 UIPageViewController 显示的自定义 View Controller 之一的示例:

//this is just a custom UILabel with some padding
@property (nonatomic) PaddedLabel *directionsLabel;

//I have tried setting UILabel to nil or removing it from view
-(void)viewWillDisappear:(BOOL)animated
{

NSLog(@"view is disappearing");
//this does not remove the label
self.directionsLabel = nil;
//nor does this
[self.directionsLabel removeFromSuperview];

}

- (void)viewDidAppear:(BOOL)animated
{
[self messageTutorial];
}

- (void)messageTutorial
{

CGFloat width = [[UIScreen mainScreen] bounds].size.width;
CGFloat height = [[UIScreen mainScreen] bounds].size.height;
PaddedLabel *directionsLabel = [[PaddedLabel alloc] initWithFrame:CGRectMake(_width/2, _height/2, 100, 100)];
directionsLabel.text = @"test";

CGRect f = directionsLabel.frame;
f.size = size;
directionsLabel.frame = f;
directionsLabel.center = self.view.center;
f = directionsLabel.frame;
f = directionsLabel.frame;
f.origin.y = .1*height;
directionsLabel.frame = f;
[self.view addSubview:directionsLabel];


[UIView animateWithDuration:TUTORIAL_DISAPPEAR_TIME animations:^{
directionsLabel.alpha = .5;
CGRect f = directionsLabel.frame;
f.origin.y = height - f.size.height*1.4;
directionsLabel.frame = f;
NSLog(@"animating");

} completion:^(BOOL finished) {
[directionsLabel removeFromSuperview];
//this also doesn't actually remove the label

}];


}

问题是,如果用户返回查看此 View ​​,她现在会看到一个新标签和一个旧标签,因此,如果您来回翻页,您最终会看到很多标签都说同样的事情,在屏幕上的不同进展阶段。

如何在 View 消失时删除 UILabel 并在 View 出现/重新出现时添加/创建新的?

谢谢。

最佳答案

viewWillDisappear 方法中的代码是倒退的。你需要:

- (void)viewWillDisappear:(BOOL)animated {
NSLog(@"view is disappearing");
[self.directionsLabel removeFromSuperview];
self.directionsLabel = nil;
}

如您所见,在尝试删除它之前将 self.directionsLabel 设置为 nil 会导致无操作。

此外,请务必在创建标签时设置 self.directionsLabel

关于ios - 当 View 出现时创建一个 UILabel 并在 View 消失时销毁它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29993232/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com