2010-06-19 62 views
0

我正在嘗試編寫iphone應用程序的Objective-C中工作。如何通知對象?

背景: 有一個導航控制器管理我的視圖控制器。 ViewDidLoad上的我的FirstLevelViewController創建一些SecondLevelViewController對象,將它們存儲在一個數組中,然後在推入各種表格單元格時加載它們。另外,在viewDidLoad上,我的FirstLevelViewController創建一個類的實例來存放關於該對象的變量。這個類可能有多個實例,所以我不想創建一個單例。

各種視圖控制器想要消息數據對象。我怎麼做?第一個levelviewcontroller可以發信息給它,因爲它至少創建了第一個。第二級視圖控制器的行爲就像他們不知道數據對象存在一樣。

我知道它的基本。我知道關於生命,宇宙和一切的意義有很多知識。答案應用程序委託嗎?不是以單例數據存儲方式,而是以類之間的消息方式?我是否需要引用或指針?

我真的很感謝幫助。我已經失去了一個星期的睡眠,並失去了我的彈珠。謝謝。

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(數據類)

#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 

(基本上,我想對於更新標籤調用以更新不僅標籤時,但也是名爲弗蘭克的對象。)

回答

1

您可以從SecondLevelViewController等發送通知。數據對象或您的FirstLevelViewController然後可以註冊此類通知並根據需要執行處理。

+0

我可以直接發送通知給frank嗎? – Justagruvn 2010-06-19 22:52:23