0

私は目的の C で iPhone アプリを作成しようとしています。

背景: ビュー コントローラーを管理するナビゲーション コントローラーがあります。viewDidLoad の私の FirstLevelViewController は、いくつかの SecondLevelViewController オブジェクトを作成し、それらを配列に格納してから、さまざまなテーブル セルがプッシュされたときにそれらをロードします。さらに、viewDidLoad では、FirstLevelViewController がクラスのインスタンスを作成して、そのオブジェクトに関する変数を保持します。そのクラスには複数のインスタンスが存在する可能性があるため、シングルトンを作成しないことをお勧めします。

さまざまなビュー コントローラーがデータ オブジェクトにメッセージを送信します。それ、どうやったら出来るの?firstlevelviewcontroller は、少なくとも最初のものを作成したため、それにメッセージを送信できます。第 2 レベルのビュー コントローラーは、データ オブジェクトが存在することを知らないように動作します。

私はその基本を知っています。生命の意味、宇宙、そしてあらゆるものについて、膨大な量の知識があることを知っています。回答申請代行ですか?シングルトンデータの隠し方ではなく、クラス間でメッセージを送る方法として? 参照またはポインタが必要ですか?

本当に助かります。私はこれで 1 週間睡眠を失っており、ビー玉を失いつつあります。ありがとうございました。

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

(基本的には、ラベルだけでなく frank という名前のオブジェクトを更新するために呼び出されたときに、ラベルを更新したいと考えています。)

4

1 に答える 1

1

SecondLevelViewController などから通知を送信できます。データ オブジェクトまたは FirstLevelViewController は、そのような通知に登録し、必要に応じて処理を行うことができます。

于 2010-06-19T22:40:25.093 に答える