ios 单击某些按钮时如何重新加载我的 UIViewController?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7725215/
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
How to reload my UIViewController on click of some buttons?
提问by George
I'm developing an iPhone app, i have a UIViewController
class where some coverflow animations are there and according to design i have some 5 buttons on top of my view each button have an IBAction
method on clicking a button i need to display different set of coverflow with different data and the count of coverflow also changes from 1 button action to another. My problem is how can i refresh my page(reload) on click of these buttons, as i know calling explicitly viewDidLoad
is a very bad idea, so how can i accomplish this? Any help is appreciated in advance thank you.
我正在开发一个 iPhone 应用程序,我有一个UIViewController
类,其中有一些 Coverflow 动画,根据设计,我的视图顶部有一些 5 个按钮,每个按钮都有一个IBAction
单击按钮的方法,我需要显示不同的 Coverflow 集不同的数据和覆盖流的计数也从 1 个按钮操作更改为另一个。我的问题是如何在单击这些按钮时刷新我的页面(重新加载),因为我知道显式调用viewDidLoad
是一个非常糟糕的主意,那么我该如何实现呢?提前感谢任何帮助,谢谢。
采纳答案by Carter
The easiest way to get a view to re-draw itself is to call [UIView setNeedsDisplay] or by using [UIView setNeedsDisplayInRect]. This method tells the view that it needs to re-draw itself and it's subviews. However you shouldn't always have to call this. There are other methods that can cause views to redraw themselves. For instance if you add or remove a view to the view hierarchy using [UIView addSubview:] or [UIView removeFromSuperview].
让视图重新绘制自身的最简单方法是调用 [UIView setNeedsDisplay] 或使用 [UIView setNeedsDisplayInRect]。这个方法告诉视图它需要重新绘制自己和它的子视图。但是,您不应该总是调用它。还有其他方法可以导致视图重绘自己。例如,如果您使用 [UIView addSubview:] 或 [UIView removeFromSuperview] 在视图层次结构中添加或删除视图。
These methods cause the system to call your view's drawInRect: method (assuming you overrode it with custom drawing code). If you did not override this method for custom drawing then it should cause your subviews to receive the same message so that they can get a chance to redraw.
这些方法会导致系统调用视图的 drawInRect: 方法(假设您使用自定义绘图代码覆盖了它)。如果您没有为自定义绘图覆盖此方法,那么它应该会导致您的子视图收到相同的消息,以便他们有机会重绘。
Additional information here. Specifically read the section labeled "The View Drawing Cycle".
附加信息在这里。具体阅读标有“视图绘制周期”的部分。
回答by Oscar Gomez
Why do you want to reload the page?, if it is because you are changing your view and you want it to be redrawn, you can call:
你为什么要重新加载页面?,如果是因为你正在改变你的视图并且你希望它被重绘,你可以调用:
setNeedsDisplay();
But that is often not needed, as whenever a views bounds change that is called automatically for you.
但这通常是不需要的,因为每当视图边界发生变化时,都会自动为您调用。
回答by Tony Xu
If there's too much other stuff rather than refreshing/reloading the part of your interest in your viewDidLoad method, you can extract the refreshing part into a private method, say methodA. Every time you click your button, you call that methodA. However I think methodA still need call setNeedsDisplay or re-draw the the part of your interest. Hope it helps.
如果有太多其他内容而不是刷新/重新加载您在 viewDidLoad 方法中感兴趣的部分,您可以将刷新部分提取到私有方法中,例如 methodA。每次单击按钮时,都会调用该方法A。但是我认为 methodA 仍然需要调用 setNeedsDisplay 或重新绘制您感兴趣的部分。希望能帮助到你。
回答by Darius Miliauskas
If your point is to refresh "UIViewController", then:
如果您要刷新“UIViewController”,则:
[self viewDidLoad];
回答by NandhaKumar
To Refresh your UIViewController, When Clicking the button >>
Add the below code into your UIButton method,
-(IBAction)button:(id)sender{
[self viewDidLoad];
[self viewWillAppear:YES];
}
Now Build and Run Your Code.
回答by Sup
Use function viewWillAppear().This function is called everytime the view is called.
使用函数viewWillAppear()。每次调用视图时都会调用此函数。