开发者

Objective-C: Splitting up a class

Let's pretend I have the following three files:

  • MainViewController.h
  • MainViewController.m
  • MainViewController.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.h
  • DisplayController.m
  • DisplayController.xib

Is it possible, within MainView开发者_开发技巧Controller.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 and inited an instance of DisplayController, 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 DisplayController a pointer to a UIButton that you want to have modified somehow (say to update its text), even though that UIButton is does not "belong" to the DisplayController. Code is code, basically, and the bits don't really care who sets them.

You could even assign the pointer to that UIButton to a property in DisplayController, 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.


A .xib file is just an archive, so, yes, any class could open and read that archive. It sounds like you want to look at the

- (NSArray *)loadNibNamed:(NSString *)name owner:(id)owner options:(NSDictionary *)options

of the NSBundle class. This returns an array which is filled with the top level views, and you can navigate down the view hierarchy through the subviews of any view to find the images, labels, or whatever are contained in the .xib file.

So yes, a method in DisplayViewController could load the .xib file and do something with it, or pass it to the MainViewController.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜