reload/refresh subViews - setNeedsDisplay doesnt work
Asked Answered
E

3

5

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.

Eton answered 23/1, 2012 at 22:31 Comment(3)
Why are you doing this? What are you expecting it to do? Typically calling -setNeedsDisplay on some other UI element won't do anything, because if the UI element needs to update its display (in response to changing properties), it will have already called that method on itself.Mythopoeia
i am changing the content and also the color/image on that IBAction. the content comes from coreData. i need to redraw the uiviews to get displayed the changes on ibaction.Eton
setNeedsDisplay just redraws the view, it doesn't reset it's properties (e.g. color). To do that you'll have to reload them from the nib file (see answer below).Diabolo
D
19

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.

Diabolo answered 23/1, 2012 at 22:45 Comment(3)
Thank you for response. See my Edit1. Have explained a bit.Eton
It looks like you are doing something weird with view controllers that probably doesn't work the way you think it does. You say you don't have a view controller, but what is DetailViewController if not a view controller? I know there's no view controller in the nib file because that's not how nibs work, but you're still creating a view controller and populating it's view hierarchy from a nib.Diabolo
I have put the code in a new method and calling this method have solved my problem. Thank you!Eton
D
8

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)
Divinity answered 12/2, 2016 at 5:59 Comment(0)
L
1

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];
Liechtenstein answered 9/8, 2013 at 13:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.