Question

My View Controllers are holding my object properties and iVars etc. They shouldn't hold any at all.

** The rest of this post has been heavily edited for clarity as requested. **

My Class Object using the example given by NSBum that seeks to show data handled by one method, is able to continue being used in another.

#import <Foundation/Foundation.h>
@interface MYEmployee : NSObject

@property (nonatomic, strong) NSString *firstName;

@end

now for my VC interface file

#import <UIKit/UIKit.h>
#import "MYEmployee.h"

@interface MYEmployeeDetailViewController : UIViewController

@property ( nonatomic,strong) MYEmployee *employee;  // as suggested

// UI elements
    // a button that picks up from a textfield
- (IBAction)getName:(UIButton *)sender;

@property (strong, nonatomic) IBOutlet UITextField *inputField;

    // a button that puts the names in a label
- (IBAction)showName:(UIButton *)sender;

@property (strong, nonatomic) IBOutlet UILabel *employeeNameLabel;

    // a second label shows how i've been doing it so far
@property (strong, nonatomic) IBOutlet UILabel *contractorNameLabel;


@end

and the relevant essentials in my VC implementation file where i've used a private contractor property of the VC.

#import "MYEmployeeDetailViewController.h"

@interface MYEmployeeDetailViewController ()
@property (nonatomic, strong) NSString *contractor; 
@end

@implementation MYEmployeeDetailViewController


// MYEmployee *employee; // use of an iVar has no effect


- (IBAction)getName:(UIButton *)sender {
    // MYEmployee *employee = [[MYEmployee alloc] init]; // no effect except only within the method
    // employee.firstName = self.inputField.text;        // 

    self.employee.firstName = self.inputField.text;      // class object picks up text from a textField
    self.contractor = self.inputField.text;              // self does exactly the same
    }

- (IBAction)showName:(UIButton *)sender {
    // MYEmployee *employee = [[MYEmployee alloc] init];     // resets contents of the iVar to nil
    // self.employeeNameLabel.text = employee.firstName;     // 

    self.employeeNameLabel.text = self.employee.firstName;   // no joy using the property in .h file as suggested

    self.contractorNameLabel.text = self.contractor;         // this actually displays the text
    }

@end

the employee is not to be seen while the contractor shows up for work (typical, we call them 'Blisters' they only show up when the work is done ;)

Back to serious stuff, the [alloc[init]] and iVar lines are commented out as they had no effect, but left in anyway for the sake of completeness. I didn't think I should edit the title, but my quandry is more or less the same in that "..why can't I use the model simply as a substitution of self?" After all, I outsourced the function

Was it helpful?

Solution

I might try to narrow the focus of the question somewhat; but I'll take a shot at what I think the issues are:

My View Controllers are holding my object properties and iVars etc. They shouldn't hold any at all.

If you're following good MVC principles, then your view controllers should not have properties that would more correctly belong to the model layer. But given that view controllers are the conduit between the model and the view, certain exposed properties on the controller are essential.

Let's say you have a view controller that displays details about an employee - first name, last name, salary, department, etc. You'll probably want a model object that represents the employee. Let's call it MYEmployee. Next think about what other classes need to know about instances of MYEmployee. Those are the properties exposed in the class interface.

Now, how will our controller work with an instance of MYEmployee? Here, the view controller - let's call it MYEmployeeDetailViewController - needs to know what employee to display, so we need to expose the represented employee in the view controller's interface, e.g.

@property (nonatomic, strong) MYEmployee *employee;

Now MYEmployeeDetailViewController class users can pass an employee to display.

then how does one use Class Objects with the expectation i outlined?

I think your uncertainties revolve around questions like "who owns what data?" etc. My recommendation is to take a step back and really read the documentation on Apple's take on MVC and tap into some resources on object-oriented design. It always helps me to think first about interfaces (class interfaces, not UI) and less about implementation. Mapping out the relationships between classes before coding also helps.

OTHER TIPS

You should be calling init on your new myClass object item instead of just alloc for starters. I doubt you really need to only allocate memory but not actually initialize the object.

Beyond that, your question is a bit hard to follow. You use objects to encapsulate data, and then you expose that data to other objects that might need it. Your view controllers will have objects that handle views, and objects that handle data. The controller will get what it needs from the data objects and pass it as needed to view objects. In this way, the controller will hold the data model itself (if you are doing a proper MVC design).

You say you want your class object to hold data between methods: well, that's what instance variables are for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top