最初のコントローラは、2 番目のコントローラをインスタンス化するときに、2 番目のデリゲートが最初のビュー コントローラを指すように設定する必要があります。したがって、最初のビュー コントローラーは次のようになります。
// FirstViewController.h
#import <UIKit/UIKit.h>
@protocol FirstViewControllerDelegate <NSObject>
- (void)dealWithButton;
@end
@interface FirstViewController : UIViewController <FirstViewControllerDelegate>
@end
次のような実装を使用します。
// FirstViewController.m
#import "FirstViewController.h"
#import "SecondViewController.h"
@implementation FirstViewController
- (IBAction)goToNextViewController:(id)sender
{
SecondViewController *secondController = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
secondController.delegate = self;
[self.navigationController pushViewController:secondController animated:YES];
}
- (void)dealWithButton
{
NSLog(@"Dealt with button from second controller");
}
@end
2 番目のコントローラーは次のようになります。
// SecondViewController.h
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@class FirstViewController;
@interface SecondViewController : UIViewController
@property (weak, nonatomic) id<FirstViewControllerDelegate> delegate;
@property (weak, nonatomic) IBOutlet UILabel *label;
- (IBAction)buttonPressed:(id)sender;
@end
次のような実装を使用します。
// SecondViewController.m
#import "SecondViewController.h"
@implementation SecondViewController
@synthesize delegate = _delegate;
@synthesize label = _label;
- (IBAction)buttonPressed:(id)sender
{
int random_num;
random_num = (arc4random() % 5 - 1) + 1;
if (random_num == 1)
self.label.text = @"hello1";
else if (random_num == 2)
self.label.text = @"hello2";
else if (random_num == 3)
self.label.text = @"hello3";
else if (random_num == 4)
self.label.text = @"hello4";
[self.delegate dealWithButton];
}
@end
アップデート:
最初の質問では、ラベルを最初のコントローラーに配置するか、2 番目のコントローラーに配置するかが明確ではありませんでした。上記の私の回答では、2 番目のコントローラーでそれが必要であると想定していましたが、振り返ってみると、最初のコントローラー (デリゲート) でそれが必要だった可能性があります。もしそうなら、次のコードはそれを行います。ビューdealWithButton
が表示されているかどうかがわからないため危険です ( を受け取った場合はアンロードされている可能性がありますdidReceiveMemoryWarning
)。だから私は待っていviewWillAppear
ます。繰り返しますが、最初のView Controllerは次のとおりです。
// FirstViewController.h
#import <UIKit/UIKit.h>
@protocol FirstViewControllerDelegate <NSObject>
- (void)dealWithButton;
@end
@interface FirstViewController : UIViewController <FirstViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
そしてその実装:
// FirstViewController.m
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()
{
NSString *_labelText;
}
@end
@implementation FirstViewController
@synthesize label = _label;
// if you're using storyboards, it would be like:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"delegateSegue"])
{
SecondViewController *destinationController = segue.destinationViewController;
FirstViewController *sourceController = segue.sourceViewController;
destinationController.delegate = sourceController;
}
}
// if not using storyboards, you probably have a button like:
- (IBAction)goToNextViewController:(id)sender
{
SecondViewController *secondController = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
secondController.delegate = self;
[self.navigationController pushViewController:secondController animated:YES];
}
- (void)dealWithButton
{
// note, because this is being called by the second view controller, you should *not* update the UI
// directly, because you can't be assured this view controller's view is still in memory (if you got
// a didReceiveMemoryWarning while on the second view controller, this first view controller will
// stay in memory, but its view could have been released). So save what you want the label to be,
// and update it on viewWillAppear (and if the view was released, it will be reloaded by the time
// you hit viewWillAppear.
//
// clearly, if you were doing view controller containment and this was the parent view, you wouldn't
// want to do this. But I assume you're dealing with a simple push/present view controller situation.
int random_num;
random_num = (arc4random() % 5 - 1) + 1;
if (random_num == 1)
_labelText = @"hello1";
else if (random_num == 2)
_labelText = @"hello2";
else if (random_num == 3)
_labelText = @"hello3";
else if (random_num == 4)
_labelText = @"hello4";
NSLog(@"Dealt with button from second controller");
}
- (void)viewWillAppear:(BOOL)animated
{
self.label.text = _labelText;
}
@end
そして2番目のView Controller:
// SecondViewController.h
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@class FirstViewController;
@interface SecondViewController : UIViewController
@property (weak, nonatomic) id<FirstViewControllerDelegate> delegate;
- (IBAction)buttonPressed:(id)sender;
@end
そしてその実装:
// SecondViewController.m
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
@synthesize delegate = _delegate;
- (IBAction)buttonPressed:(id)sender
{
[self.delegate dealWithButton];
}
@end