reload/refresh subViews - setNeedsDisplay doesnt work

33.7k views Asked by At

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..)

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?

EDIT 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:

  • 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. *

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.

With IBAction i want to iterate the coreData rows, display the next row in current if the action is called..and so on..

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.

EDIT 2: SOLVED
I have put the code in a new method and calling this method have solved my problem.

3

There are 3 answers

3
Nick Lockwood On BEST ANSWER

Are you trying to reset your view and subviews to their default state in the nib file?

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.

0
DogCoffee On

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];
0
smoosh911 On

Swift 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)