Reload / refresh subViews - setNeedsDisplay not working

I'm having problems with setNeedsDisplay. I have a UIView with a lot of sub UIViews created in Inteface Builder. I also have a button with IBAction. In this IBAction, I want to redraw / reload all UIViews (or all UIElements, e.g. UILabel, UIWebView, etc.).

I do this, which does not work for me, I don’t 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 to redraw / reload / update all UIElements / UIViews and Subviews?

EDIT 1:

How to load a view:

 detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController_iPad" bundle:[NSBundle mainBundle]]; 

There is no view manager in the detailview controller, just uiviews, here is the hierarchy:

  • Uiview
    - UIViewContainer
    --- UIView0
    --- UIView1
    --- UIViewStapel

What I expect to do:

* I do not want to reset UIView, I want to change the background and their contents. *

In my detailed view, I have many subheadings (uiviews), see hierarchy above. I show the content in UIViews, the content comes from coreData.
View1 contains the current coreData row.
View0 contains the previous line of coreData.
ViewStapel contains the following line coreData.

With IBAction, I want to iterate over the coreData strings, display the next row in the current one if the action is called ... and so on.

In the log, data changes but is not displayed in UIViews. To do this, I need to redraw or reload or something like this to display the current data.

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

+4
source share
3 answers

Are you trying to reset your browsing and subtitles to their default state in the nib file?

If so, then the approach described above will not work. The only way to do this automatically is to reload the views from nib, but you will need to know more clearly how you load the view first, before I can help.

If you upload it to the view controller, then the easiest way to update it is to remove the screen controller from the screen and recreate it, but you can reload the view saying something like this (from inside 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 load a view directly from nib using [[NSBundle mainBundle] loadNibNamed ...] or the equivalent, then the best way to reload the view is to simply throw it away and call this method again.

+17
source

Swift 2

This is a replica of Nick Lockwood's answer for a small copy.

 let parent = self.view.superview self.view.removeFromSuperview() self.view = nil parent?.addSubview(self.view) 
+6
source

This may help someone with swap in container view. The above code gave me an idea to try this and it works.

 [self.pageViewController.view removeFromSuperview]; self.pageViewController.view = nil; // unloads the view [self performSegueWithIdentifier:@"pageViewSegue" sender:self]; 
+1
source

Source: https://habr.com/ru/post/1392567/


All Articles