Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>[EDIT/UPDATE -- I have abandoned the approach described below in favor of using categories, as mentioned in some of the other answers. My typical situation is that as a view has control components added, the View Controller file becomes unwieldy as code is added to accommodate the various delegate and data source methods for the controls. Now I add in code stubs in the view controller file, then implement them for real in a class category. For instance, I might have an AlbumViewController screen that has a search bar and a collection view, so I create categories AlbumViewController+SearchBar and AlbumViewController+CollectionView. This allows the View Controller class to stay at a reasonable size, without incurring any of the drawbacks I listed below for included files. The only downside is that any instance variables, ie properties, must be declared publicly for a category to access them.]</p> <p>I also want to split my files up, but think categories are not the correct solution in some cases. For example, as nibs grow to include multiple objects (such as tables, buttons, nav bars, tabs, etc) the need for placing all the supporting methods in the viewcontroller.m file can lead to a very large and unwieldy file.</p> <p>For this discussion, I am going to refer to the original/standard .m file as the parent, and the subsidiary .m files as children. </p> <p>The goal is to have a single parent .h and .m, and multiple child .m files each of which can be edited independently, but compiled as if all the child .m files were in the parent .m file. </p> <p>This would be useful if one wants the ability to, for instance, place all table related methods in a file, edit it, and then have it compile as if it were included in the viewcontroller.m implementation file. It seems this is possible, but requires a little effort, and has one (possibly serious) drawback.</p> <p>The thing to keep in mind is that there are two distinct tools being used: the IDE (which provides intelligent source editing) and the compiler/make system which turns a project's source code into a runnable app.</p> <p>To get the IDE to work as expected, the child files need to appear to be part of the implementation of the class. This can be accomplished by wrapping the @implementation and @end directives in conditional compilation macros. When editing a file on its own, the IDE considers the child files to be the body of the class, but the compiler doesn't.</p> <p>To get the compiler to not complain, you can't have the child files considered part of the target -- instead they get pulled in through the preprocessor #include directive. This can be accomplished by not adding them to the target when the file is created (or added to the project), or by removing them on the "Build Phases" -> "Compile Sources" pane.</p> <p>You then #include the child .m files within the body of the parent .m files. The compiler loads them "in place" and compiles the source as wished for without complaints.</p> <p>The drawback of this approach (so far) is that the debugger does not recognize the child methods, and will not break on breakpoints put on them. For that reason I suggest that this approach only be used either after the code is thoroughly tested, or for code chunks that are relatively trivial and well-known, such as table delegate and data source methods.</p> <p>Here are the .h and .m files for a project with a table and a text field in a nib, with the supporting delegate methods defined in child .m files. In the nib, the interface objects are wired up normally, and have the delegates set to the file owner.</p> <p>File (parent) "MyViewController.h":</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @interface MyViewController : UIViewController @property (retain, nonatomic) IBOutlet UITableView *myTable; @property (retain, nonatomic) IBOutlet UITextField *myTextField; @end </code></pre> <p>File (parent) MyViewController.m:</p> <pre><code>#import "MyViewController.h" #define VIEW_CONTROLLER_MAIN_BODY 1 @interface MyViewController () @end @implementation MyViewController #include "MyViewController_TableMethods.m" #include "MyViewController_TextFieldMethods.m" - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)dealloc { [_myTable release]; [_myTextField release]; [super dealloc]; } </code></pre> <p>File (child) MyViewController_TableMethods.m:</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import "MyViewController.h" #ifndef VIEW_CONTROLLER_MAIN_BODY @implementation ViewController #endif #pragma mark - #pragma mark Table View Common Methods - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; { return 5; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; { static NSString *myIdentifier = @"myCellIdentifier"; static UITableViewCellStyle myStyle = UITableViewCellStyleSubtitle; UITableViewCell *cell = [self.myTable dequeueReusableCellWithIdentifier:myIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:myStyle reuseIdentifier:myIdentifier] autorelease]; } cell.textLabel.text = @"Title"; cell.detailTextLabel.text = @"Details"; cell.accessoryType = UITableViewCellAccessoryNone; return cell; } #ifndef VIEW_CONTROLLER_MAIN_BODY @end #endif </code></pre> <p>File (child) MyViewController_TextFieldMethods.m:</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import "MyViewController.h" #ifndef VIEW_CONTROLLER_MAIN_BODY @implementation MyViewController #endif - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { self.myTextField.text = @"Woo hoo!"; return YES; } #ifndef VIEW_CONTROLLER_MAIN_BODY @end #endif </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload