Benches Displays

4

Calling setNeedsDisplay:

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

Hey, here's a newbie question, but:

I have set up a .xib file with my view (of a custom class MyView). I also have a controller which controls two buttons and labels. I want to call setNeedsDisplay from the controller (after clicking the button) but I cannot figure out how to do it, as I believe neither the controller or the view will be created (so I can refer to them) untill runtime (I'm probably very wrong). So how the heck do I call it?

Hopefully one of you can point me in the right direction! B.T.W I'm doing assignment 3 from the Stanford podcast on iPhone dev ( Lecture 4 -
http://www.stanford.edu/class/cs193p/cgi-bin/index.php).

Thanks!!

4 replies

Sign in or join Beachball Help to reply.

  1. 0

    RayNewbie

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hi Anthony - I'm not sure I know what you mean. How would you call setNeedsDisplay before runtime? Did something in the assignment lead you to believe IB would execute your code? Maybe I'll look up the assignment and try to get a better idea.

  2. 0

    Anthony Burzillo

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Sorry, what I meant was, how would I call setNeedsDisplay on the view if I only know its class name? (Or how would I find out the view's name)?

  3. 0

    RayNewbie

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Ok ..got it now.

    Yes, you must know the address of the instance of myView, not the class name. However the view object you create (actually specify to be created) in IB is just that: It's an instance of myView, just as the view controller object created in IB is an instance of UIViewController (or myViewController). So the question you want to ask is: +Where does the address of these IB-created objects show up in my code?+

    This is an excellent question, one which most beginners never ask because the templates and sample code often seem to have variables magically pre-loaded with the addresses of the corresponding view objects.

    The answer to the general question is usually one of these two:

    1) As you've seen, you can specify some of the instance variables in a custom class (such as your controller class) as
    IBOutlet. This is a flag that tells IB that those vars want to be set to the address of a UI object. So IB provides Outlet connections for those vars whenever you create an instance of that custom class. You can then "connect" the Outlets to the objects. This "connection" in IB causes an instance var to be set to the address of the corresponding IB outlet when the nib is "awakened", i.e. unarchived so that the object specifications become real allocated objects in memory.

    2) Connections to nib-created objects can be made in code. This commonly happens when you have a statement like:


    MyViewController *myController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];

    The above creates all the objects specified in MyViewController.xib, then makes all connections specified in the nib, and finally sets the myController var to the address of the "File's Owner", which should represent an instance of MyViewController.

    If MyViewController.xib was put together correctly, you can now obtain the addresses of all its objects by climbing down the tree from the File's Owner.

    For example the myController's view should have been connected to the 'view' outlet of the File's Owner, so the address of that view is now [myController view] or myController.view if view is a @property (which it is).

    Then what would be the address of that view from inside one of the controller's methods? Answer: self.view.

    how would I call setNeedsDisplay on the view

    From inside a method of myController:


    [self.view setNeedsDisplay];

    Hope that helps!

    - Ray

  4. 0

    Anthony Burzillo

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Thank you!!! It worked!