#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate> {
//NSString *globalStrings01;
}
@property (strong, nonatomic) UIWindow *window;
// プロパティ化して外部から参照できるようにします。
@property (strong, nonatomic) NSString *globalStrings01;
@end
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize globalStrings01;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
//データ受け渡しグローバルフィールド。空要素(nil)を追加する
globalStrings01 = nil;
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
- (void)applicationWillTerminate:(UIApplication *)application
{
}
@end
#import <UIKit/UIKit.h>
@interface DelegateAViewController : UIViewController{
//NSString *globalStrings01;
}
//グローバルフィールド入力
@property (weak, nonatomic) IBOutlet UITextField *globalStrings01;
// データ受け渡しアクションボタン
- (IBAction)Second:(id)sender;
@end
#import "DelegateAViewController.h"
// Delegate画面遷移用
#import "AppDelegate.h"
@interface DelegateAViewController ()
@end
@implementation DelegateAViewController{
}
@synthesize globalStrings01;
- (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.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - appDelegateに転送
- (IBAction)Second:(id)sender {
// delegateデータを送る準備
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
// appDelegate.globalStrings01 転送
appDelegate.globalStrings01 = self.globalStrings01.text;
//ソフトウェアキーボードの非表示
[self.globalStrings01 resignFirstResponder];
}
@end
#import <UIKit/UIKit.h>
@interface DelegateBViewController : UIViewController
// ローカルラベル表示用
@property (weak, nonatomic) IBOutlet UILabel *localLabel01;
@end
#import "DelegateBViewController.h"
// Delegate画面遷移用
#import "AppDelegate.h"
@interface DelegateBViewController ()
@end
@implementation DelegateBViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
#pragma mark - appDelegateに転送
// View が表示される直前に呼ばれるメソッド
// タブ等の切り替え等により、画面に表示されるたびに呼び出されます。
// タブが切り替わるたびに何度でも呼ばれます。
/*
iPhone開発 UIViewController ライフサイクル viewDidLoad viewWillAppear viewDidAppear viewWillDisappear viewDidDisappear ios 逆引き サンプル
*/
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// delegateデータを受ける
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
self.localLabel01.text = appDelegate. globalStrings01;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
GitHub AppDelegateSample
コメントをお書きください