ios 重新加载/刷新子视图 - setNeedsDisplay 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8979585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
reload/refresh subViews - setNeedsDisplay doesnt work
提问by brush51
i have trouble with setNeedsDisplay. I have an UIView with a lot of sub UIViews, created in Inteface Builder. I have also an Button with IBAction. In this IBAction i want to redraw/reload all UIViews (or all UIElements, like UILabel, UIWebView and so on..)
我在使用 setNeedsDisplay 时遇到问题。我有一个带有很多子 UIView 的 UIView,它们是在 Inteface Builder 中创建的。我还有一个带有 IBAction 的按钮。在此 IBAction 中,我想重绘/重新加载所有 UIViews(或所有 UIElements,如 UILabel、UIWebView 等..)
I am doing this, which doesn't work for me, dont know why.. :
我正在这样做,这对我不起作用,不知道为什么..:
//redraw the UIViews
[self.view_card_0 setNeedsDisplay];
[self.view_card_1 setNeedsDisplay];
[self.view_card_stapel setNeedsDisplay];
//other UI Elements
[self.webView_0 setNeedsDisplay];
[self.webView_1 setNeedsDisplay];
[self.webView_stapel setNeedsDisplay];
[self.lblCardTitle_0 setNeedsDisplay];
[self.lblCardTitle_1 setNeedsDisplay];
[self.lblCardTitle_stapel setNeedsDisplay];
[self.img_card_0 setNeedsDisplay];
[self.img_card_1 setNeedsDisplay];
[self.img_card_stapel setNeedsDisplay];
What to do for redraw/reload/refresh all UIElements/UIViews and Subviews?
如何重绘/重新加载/刷新所有 UIElements/UIViews 和 Subviews?
EDIT 1:
编辑 1:
How i load the view:
我如何加载视图:
detailViewController = [[DetailViewController alloc]
initWithNibName:@"DetailViewController_iPad" bundle:[NSBundle mainBundle]];
The detailviewcontroller has no viewcontroller, just uiviews, here the hierarchie:
detailviewcontroller 没有 viewcontroller,只有 uiviews,这里是层次结构:
- UIView
-- UIViewContainer
--- UIView0
--- UIView1
--- UIViewStapel
- UIView
-- UIViewContainer
--- UIView0
--- UIView1
--- UIViewStapel
What i expect to do:
我希望做什么:
*I dont want to reset the UIView, i want to change the background and the content of them. *
*我不想重置 UIView,我想更改背景和它们的内容。*
I have in my detailview a lot of subviews (uiviews), see above hierarchie. I am displaying content in the UIViews, the content comes from coreData.
The view1 contains the current row of coreData.
The view0 contains the previous row of coreData.
The viewStapel contains the next row of coreData.
我的 detailview 中有很多子视图(uiviews),请参见上面的层次结构。我在 UIViews 中显示内容,内容来自 coreData。
view1 包含 coreData 的当前行。
view0 包含 coreData 的前一行。
viewStapel 包含 coreData 的下一行。
With IBAction i want to iterate the coreData rows, display the next row in current if the action is called..and so on..
使用 IBAction 我想迭代 coreData 行,如果调用该操作,则显示当前的下一行..等等..
In Log, the data is changed, but not displayed in the UIViews. Cause of this i need a redraw or reload or something like this, to get displayed the current data.
在 Log 中,数据已更改,但未显示在 UIViews 中。原因是我需要重绘或重新加载或类似的东西,以显示当前数据。
EDIT 2: SOLVED
I have put the code in a new method and calling this method have solved my problem.
编辑 2:已解决
我已将代码放入新方法中,调用此方法解决了我的问题。
回答by Nick Lockwood
Are you trying to reset your view and subviews to their default state in the nib file?
您是否尝试将您的视图和子视图重置为它们在 nib 文件中的默认状态?
If so the approach above won't work. The only way to do that automatically is to re-load the views from the nib, but you'll need to be clearer about how you are loading the view in the first place before I can help.
如果是这样,上面的方法将不起作用。自动执行此操作的唯一方法是从笔尖重新加载视图,但是在我提供帮助之前,您首先需要更清楚地了解如何加载视图。
If you are loading it into a view controller then the easiest way to refresh it is to remove the view controller from screen and re-create it, but you might be able to reload the view saying something like this (from within the controller):
如果您将其加载到视图控制器中,那么刷新它的最简单方法是从屏幕中删除视图控制器并重新创建它,但您可能能够重新加载视图,说这样的话(从控制器内部):
UIView *parent = self.view.superview;
[self.view removeFromSuperview];
self.view = nil; // unloads the view
[parent addSubview:self.view]; //reloads the view from the nib
If you are loading the view directly from a nib using [[NSBundle mainBundle] loadNibNamed...] or equivalent, then best way to reload the view is just to throw it away and call that method again.
如果您使用 [[NSBundle mainBundle] loadNibNamed...] 或等效方法直接从笔尖加载视图,那么重新加载视图的最佳方法就是将其丢弃并再次调用该方法。
回答by smoosh911
Swift 2
斯威夫特 2
This is a replica of Nick Lockwood's answer for some easy copy paste
这是尼克洛克伍德的答案的复制品,用于一些简单的复制粘贴
let parent = self.view.superview
self.view.removeFromSuperview()
self.view = nil
parent?.addSubview(self.view)
回答by DogCoffee
This might help someone, with paging within a container view. The above code gave me the idea to try this and it works.
这可能会帮助某人,在容器视图中进行分页。上面的代码给了我尝试这个的想法并且它有效。
[self.pageViewController.view removeFromSuperview];
self.pageViewController.view = nil; // unloads the view
[self performSegueWithIdentifier:@"pageViewSegue" sender:self];