-2

I'm trying to learn how classes work and I've hit a rut. The string is saving to my place class in viewcontroller A, but it's accessing a null in viewconrollerB. Can someone explain why this is happening? Like I said I'm very unfamiliar with classes and trying to learn.

Code from view controller A (When you click on a button the name associated with that button is saved)

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    self.place=[[Place alloc]init];
    return self;
}

- (void) buttonPressed:(UIButton *) sender
{
    NSLog(@"works");

    self.place.name=self.name;
    NSLog(@"%@",self.place.name); 
}

The Class.m

@implementation Place
-(id)init {
    self=[super init];
    return self;
}
@end

viewController B:

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        self.title=@"Shack";
    }
        self.place=[[Place alloc]init];
    return self;
}
- (void)viewWillAppear:(BOOL)animated {

    NSLog(@"%@",self.place.name); 

}

The Class.h

@interface Place : NSObject

@property (strong,nonatomic) NSString *name;

@end

Viewcontrollerb.h

#import "Place.h"

@interface FavoritesTableViewController : UITableViewController
@property (nonatomic) NSString * name;
@property (strong,nonatomic) Place * place; 
@end

ViewcontrollerA.h

@interface MoreViewController : UIViewController
 @property (nonatomic,strong) NSString * name;
@property (nonatomic,strong) Place *place;
@end
Blake
  • 63
  • 10
  • You don't understand what a class is. Please spend some time studying object-oriented principles. – Hot Licks Jun 24 '14 at 15:51
  • possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Duncan C Jun 24 '14 at 16:38
  • Variations on "How do I pass data/objects between view controllers" are one of the most common newbie questions for iOS. You should do a little searching before posting such a trivial question. See this link, for example: http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Duncan C Jun 24 '14 at 16:39

1 Answers1

0

In ViewControllerB, you create a new Place in your initwithstyle. This place is different from the Place created in your ViewControllerA. Therefore, when you access the name property, you access a null. To fix this, you need to set the place on your 2nd vc to that on the 1st one. This will create a pointer which allows you to access the original name.

gram
  • 26
  • 4
  • I thought that the initialization of the class sets them equal. Like my 2nd one is just a duplicate of my first one. – Blake Jun 24 '14 at 15:54
  • Not quite. Think of a class like a blueprint for objects. When you call alloc, you instruct the ObjC runtime (the factory) to make a new object. However, when you set two object equal, you tell the factory "I don't need another! Give me the old" – gram Jun 24 '14 at 16:09