私は、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 がシミュレーターに表示されます。プロファイル モデルが使用されている場合、アプリケーションはビルドされ、エラーや警告は発生しませんが、情報は表示されません。私は非常に単純なものを見落としていると確信しているので、どんな助けでも大歓迎です。ありがとう。