views:

30

answers:

1

I am working in objective-C trying to write an iphone app.

Background: There is a navigation controller managing my view controllers. My FirstLevelViewController on viewDidLoad creates a few SecondLevelViewController Objects, stores them in an array, and then loads them when various table cells are pushed. In addition, on viewDidLoad, my FirstLevelViewController creates in instance of a class to hold variables about that object. There could be multiple instances of that class, and so i would prefer not to create a singleton.

The various view controllers would like to message the data object. How do I do that? The firstlevelviewcontroller can message to it because it created at least the first one. The second level view controllers act like they don't know that data object exists.

I know its basic. I know there is a large amount of knowledge about the meaning of life, the universe, and everything. Is the answer application delegate? Not in the singleton data stash way, but as a way to message between classes? Do i need references, or pointers?

I really appreciate the help. I have been losing sleep over this for a week and am losing my marbles. Thank you.

FirstLevelViewController

#import <Foundation/Foundation.h>

@interface FirstLevelViewController : UITableViewController 
<UITableViewDataSource, UITableViewDelegate> {
NSArray *controllers;
}

@property (nonatomic, retain) NSArray *controllers; 


@end


#import "FirstLevelViewController.h"
#import "BirthDay.h"
#import "Height.h"
#import "Model.h"


@implementation FirstLevelViewController
@synthesize controllers; 

-(void)viewDidLoad {


NSMutableArray  *array = [[NSMutableArray alloc]init];

Model *frank = [[Model alloc]init]; 
frank.date = @"Oh No You Didn't"; 
self.title = [frank date]; 


BirthDay *birthday = [[BirthDay alloc]initWithNibName:@"Birthday" bundle:nil]; 
birthday.title = @"Birth Date"; 

[array addObject:birthday];
[birthday release]; 

Height *height = [[Height alloc]initWithNibName:@"Height" bundle:nil]; 
height.title = @"Height"; 
[array addObject:height]; 
[height release];   

self.controllers = array; 

ModelClass (Data class)

#import <Foundation/Foundation.h>
#import "FirstLevelViewController.h"
#import "BirthDay.h"
#import "model.h"


@interface Model : NSObject {
    NSString *date; 
}

@property (nonatomic, retain) NSString *date; 
@end


#import "Model.h"

@implementation Model
@synthesize date; 


@end

SecondLevelViewController

@interface BirthDay : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>{

    UILabel *dateLabel;
    UIPickerView *datePicker; 


}

@property (nonatomic, retain) IBOutlet UILabel *dateLabel; 
@property (nonatomic, retain) IBOutlet UIPickerView *datePicker; 

@end


#import "BirthDay.h"
#import "FirstLevelViewController.h"
#import "Model.h"

@implementation BirthDay
@synthesize datePicker;
@synthesize dateLabel; 

-(IBAction)updateLabel {

    NSDate *dateOfBirth = [datePicker date]; 
    NSDate *todaysDate = [NSDate date]; 

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit; 

    NSDateComponents *components = [gregorian components:unitFlags fromDate:dateOfBirth toDate:todaysDate options:0]; 
    NSInteger months = [components month]; 
    NSInteger days = [components day];

    float Months = months; 
    if (days > 14) {
        Months = Months + 0.5; 
    }




    NSString *message = [[NSString alloc]initWithFormat: @"%.1f Months and %d Days old", Months, days];

    dateLabel.text = message; 

}

@end

(Basically, I would like for update label when called to update not only the label but also the object named frank.)

+1  A: 

You could send notifications from your SecondLevelViewController and alike. The data object or your FirstLevelViewController could then register for such notifications and do the processing as necessary.

E-ploko
Could I send notifications directly to frank?
Justagruvn