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