#import "ViewController.h"
#import "ViewController02.h" // ヘッダーファイル定義(デリゲート使用 1/4)
#import "ViewController03.h"
@interface ViewController ()
<ViewController02Delegate> // プロトコル実装(デリゲート使用 2/4)
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// 他ビュー遷移時
- (void)prepareForSegue:(UIStoryboardSegue *)segue
sender:(id)sender
{
// セグエ判定
if ([segue.identifier isEqualToString:@"SegueTo02"]) {
NSLog(@"View02行ってきます");
ViewController02 *vw = (ViewController02 *)
segue.destinationViewController;
vw.tempMsg02 = @"View01からのメッセージデータです。";
// 設定(デリゲート)(デリゲート使用 4/4)
vw.delegate = self;
} else if ([segue.identifier isEqualToString:@"SegueTo03"]){
NSLog(@"View03行ってきます");
ViewController03 *vw = (ViewController03 *)
segue.destinationViewController;
vw.tempMsg03 = @"View01からのメッセージデータです。";
}
}
// 他ビュー終了時(UnWind Segue(iOS6-))View03
- (IBAction)returnView01:(UIStoryboardSegue *)segue {
if ([segue.identifier isEqualToString:@"SegueFrom03"]) {
// 遷移先インスタンス取得
ViewController03 *vw = (ViewController03 *)
segue.sourceViewController;
// 空っぽで良い
NSLog(@"%@",vw.tfMessage.text);
}
}
#pragma mark - ViewController02Delegate Methd
// メソッドの実装(デリゲート使用 3/4)
-(void)ViewController02BeforeExit:(ViewController02 *)view {
//NSLog(@"%s", __func__);
NSLog(@"%@", view.tfMessage.text);
}
@end
#import <UIKit/UIKit.h>
// プロトコルのプロトタイプ宣言(デリゲート作成 2/4)
@protocol ViewController02Delegate;
@interface ViewController02 : UIViewController
//-----------------
@property (weak, nonatomic) NSString *tempMsg02;
//
@property (weak, nonatomic) IBOutlet UITextField *tfMessage;
//-----------------
// デリゲートのプロパティ定義(デリゲート作成 3/4)
@property (weak, nonatomic) id<ViewController02Delegate> delegate;
@end
// プロトコル定義(通信)(デリゲート作成 1/4)
@protocol ViewController02Delegate <NSObject>
@required // 必須(デフォルト)
// 画面終了時のメソッド
- (void)ViewController02BeforeExit:(ViewController02 *)view;
@optional // 任意
@end
//#import "ViewController.h"
#import "ViewController02.h"
//#import "ViewController03.h"
@interface ViewController02 ()
@end
@implementation ViewController02
//- (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.
// View01から値反映
self.tfMessage.text = self.tempMsg02;
[self.tfMessage resignFirstResponder];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
// 値1 キーボードを隠す Did End On Exit と併用している
- (IBAction)didEndValue01:(id)sender
{
}
// 戻るボタンReturn
- (IBAction)backView01:(id)sender {
// デリゲートメソッド呼び出し(デリゲート作成 4/4)
[self.delegate ViewController02BeforeExit:self];
// ビュー閉じる(終わらせる)
[self dismissViewControllerAnimated:YES
completion:nil];
}
@end
#import <UIKit/UIKit.h>
@interface ViewController03 : UIViewController
@property (weak, nonatomic) NSString *tempMsg03;
// View01に戻るための記述
@property (weak, nonatomic) IBOutlet UITextField *tfMessage;
@end
#import "ViewController03.h"
#import "ViewController.h"
@interface ViewController03 ()
@end
@implementation ViewController03
- (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.
NSLog(@"%@",self.tempMsg03);
// View01から値反映
self.tfMessage.text = self.tempMsg03;
[self.tfMessage resignFirstResponder];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Navigation
/*
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
NSLog(@"行ってきます");
// セグエ判定
if ([segue.identifier isEqualToString:@"SegueTo03"]) {
NSLog(@"View02行ってきます");
ViewController *vw = (ViewController *)
segue.destinationViewController;
vw.tempMsg = @"View01からのメッセージデータです。";
} else if ([segue.identifier isEqualToString:@"SegueT03"]){
NSLog(@"View03行ってきます");
}
}
*/
// 値1 キーボードを隠す Did End On Exit と併用している
- (IBAction)didEndValue01:(id)sender
{
}
// 戻るボタンReturn
- (IBAction)backView01:(id)sender {
// ビュー閉じる(終わらせる)
[self dismissViewControllerAnimated:YES
completion:nil];
}
@end
// 値1 キーボードを隠す Did End On Exit と併用している
- (IBAction)didEndValue01:(id)sender
{
}
GitHub Storyboard
▫️参考ページ
コメントをお書きください