So I have a class defined in Foo.h, implemented in Foo.m. I then have a controller called FooController.h/m that has multiple methods to interface Foo with the view.
Foo has multiple properties so I’m only gonna refer to three defined as
// Foo.h
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSNumber *level;
@property (strong, nonatomic) Foo1 *foo1;
As you can see, Foo has a property of class Foo1 which I defined elsewhere. I didn’t complete the implementation of Foo1 but basically it’s just a set of more properties that are defined. (I didn’t define any init method for Foo1. Could that be a problem?) One property of Foo1 that you should keep in the back of your mind is:
// Foo1.h
@property (strong, nonatomic) NSString*name;
The following Foo methods are defined:
// Foo.m
-(id) initWithName:(NSString *)name
level:(NSNumber *)level
foo1:(Foo1 *)foo1
{
self = [super init];
if (self) {
_name = name;
_level = level;
_foo1 = foo1;
return self;
}
return nil;
}
One of the methods that FooController has is called makeRandomFoo that generates a generic name, random level, and a statically defined instance of foo1 (static in the sense of size). It’s implementation is as follows:
// FooController.h
#import "Foo.h"
#import "Foo1.h"
@interface FooController : NSObject
@property (strong, nonatomic) Foo *foo;
- (Foo *)makeRandomFoo:(Foo *)foo;
// FooContoller.m
#import <stdlib.h>
@implementation FooController
@synthesize foo = _foo;
- (Foo *)makeRandomFoo:(Foo *)foo
{
NSString *name = @"Random Foo 1";
NSNumber *level = [NSNumber numberWithInt:(rand() / 100)];
Foo1 *foo1 = [[foo1 alloc] init];
foo = [[foo alloc] initwithName:name
atLevel:level
withFoo1:foo1];
return foo;
}
Then on my viewcontroller, FooViewController.h/m, I created a round rect button called “Make Random Foo” and three labels called “nameLabel”, “levelLabel” “foo1Label”
Before I show “Make Random Foo”‘s method (bottom of the implementation), I should just show the definition and implementation of my vc:
// FooViewController.h
@class Foo;
@class FooController;
@interface FooViewController : UIViewController
@property (strong, nonatomic) Foo *foo;
@property (strong, nonatomic) FooController *fooController;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *levelLabel;
@property (weak, nonatomic) IBOutlet UILabel *foo1Label;
- (IBAction)randomizeButton;
// FooViewController.m
- (id)reloadView
{
self.nameLabel.text = self.pet.name;
self.levelLabel.text = [NSString stringWithFormat:@"%@",self.pet.level];
self.foo1Label.text = self.pet.foo1.name;
(remember when I told you about a property of foo1 called name?)
}
- (IBAction)randomizeButton
{
[self.petController makeRandomFoo:self.foo];
[self reloadView];
}
So when I run the code and hit the random button, the name and foo1 label remains blank and the level label says (null).
All properties are synthesized but I didn’t write and custom setters or getters for them.
Could you help me out?
I tried being as descriptive as I could but if you have more questions, feel free to ask away!
You are not following memory management rules as you are assigning the initialisation parameters directly to your instance variables within
[Foo init]. You need to use your (synthesized) setter methods instead:As a consequence of your implementation the instance variables are not being retained and are therefore probably being released prematurely. We don’t want premature anything now do we?
EDIT To implement your setter methods (that have been declared using the
@propertykeyword in the header file), you simply use@synthensizethe same way you have done inFooControlleralready:EDIT 2: After a bit more looking, you seem to be calling the
[Foo init...]method with differently named parameters. You also need to release theFoo1object as you have passed ownership toFoo. Also conventionally you need to start this method with the namenewas you are creating and returning a new instance ofFoo.Try this and set a breakpoint at the last line and see if you can inspect the properties of
Foo: