0

私は、MVC モデルでユーザー情報を表示する簡単なプログラムの作成に取り組んできました。すべてのデータがビュー コントローラー上にある場合、アプリケーションは完全に機能しますが、データをプロファイル モデルに移動しようとすると、アプリケーションは正常にビルドされますが、情報は表示されません。これが私が今取り組んでいるコードです。

プロファイル ビュー コントローラーのヘッダー:

DefaultProfileViewController.h

#import <UIKit/UIKit.h>
#import "Profile.h"

@interface DefaultProfileViewController : UIViewController

@property (strong, nonatomic) IBOutlet UILabel *fullNameLabel;
@property (nonatomic, strong) Profile *profile;

@end

プロファイル ビュー コントローラーの実装

#import "DefaultProfileViewController.h"

@class Profile;

@interface DefaultProfileViewController ()

@end

@implementation DefaultProfileViewController

@synthesize profile = _profile;

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

- (void)viewDidLoad
{
    [super viewDidLoad];
        // Do any additional setup after loading the view.

    _fullNameLabel.text = _profile.fullName;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

プロファイル モデル ヘッダー

#import <Foundation/Foundation.h>

@interface Profile : NSObject

@property (nonatomic, copy) NSString *fullName;

- (void)loadProfile:(NSString *)fullName;

@end

そして Profile モデルの実装

#import "Profile.h"

@implementation Profile

@synthesize fullName = _fullName;

- (void)loadProfile:(NSString *)fullName
{
    _fullName = @"Full Name";
}

@end

前述のように、_fullName = @"Full Name" の部分がコントローラーで使用されている場合、これは問題なく機能し、テキストの Full Name がシミュレーターに表示されます。プロファイル モデルが使用されている場合、アプリケーションはビルドされ、エラーや警告は発生しませんが、情報は表示されません。私は非常に単純なものを見落としていると確信しているので、どんな助けでも大歓迎です。ありがとう。

4

1 に答える 1

1

コードサンプルはプロパティを宣言しますprofileが、実際にプロファイルを設定する場所はありません。

Objective-C 2.0 で宣言されたプロパティが1 つのことを行うことを思い出してください。それは、「背後で」アクセサ メソッドを作成することです。(「昔」は、アクセサ メソッドを手作業で書かなければなりませんでした。面倒ですが、メモリ管理は非常に得意です!)

あなたの場合、インスタンス化するクラスがオブジェクトDefaultProfileViewControllerを作成し、新しいインスタンスProfileを呼び出す必要がある可能性があります。setProfileDefaultProfileViewController

このようなもの:

// create Profile object
Profile *aProfile = [[Profile alloc] init];
[aProfile loadProfile:@"John Doe"];
DefaultProfileViewController *vc = [[DefaultProfileViewController alloc] initWithNibName:@"your nib name" bundle:nil];

// set the profile on your view controller
vc.profile = aProfile;

// add your view controller's view to the view hierarchy
// however you are doing that now...

注、ARC を想定しています。

于 2012-12-09T00:05:34.247 に答える