Let’s pretend I have the following three files:
MainViewController.hMainViewController.mMainViewController.xib
The MainViewController.xib will display a lot of different information to the screen, such as buttons, labels and images.
Let’s also pretend that I also have these three files:
DisplayController.hDisplayController.mDisplayController.xib
Is it possible, within MainViewController.m, to call a method in DisplayController.m that is able to change what is being displayed on MainViewController.xib?
So, if I want a separate class that will draw the images to MainViewController.xib, and a separate class that will draw the buttons to MainViewController.xib, how would I go about that? I would imagine that there would need to be some sort of link established between MainViewController and DisplayController so that DisplayController was able to manipulate attributes found in MainViewController.
Any help would be greatly appreciated!
First off, you can call a static (ie, “+” vs “-“) method from essentially anywhere at any time. This is sometimes the simplest/best way to implement common methods that don’t need access to instance variables.
Next, if you have
alloced andinited an instance ofDisplayController, and you have a pointer to that instance, you can call methods of that instance, even though the view it controls is not being displayed.And you can, eg, pass into an instance method of
DisplayControllera pointer to aUIButtonthat you want to have modified somehow (say to update its text), even though thatUIButtonis does not “belong” to theDisplayController. Code is code, basically, and the bits don’t really care who sets them.You could even assign the pointer to that
UIButtonto a property inDisplayController, so that you could come back later and manipulate it without needing to have the pointer passed in again.You just need to be careful and not forget that you’re dealing with a “foreign” object, and that your current instance variables (other than perhaps its own property) have no relation to it. It’s wise to have some highly-visible comments explaining this.
What you probably can’t do (to my knowledge) is somehow link your DisplayController directly to MainViewController.xib (while that xib is simultaneously linked by MainViewController and/or your DisplayController instance is simultaneously linked to DisplayController.xib). Yeah, there maybe are hokey ways to sort of do it, but they’d be fragile, complex, and error-prone.