2 つのビュー コントローラ間で通信するためのシンプルなデリゲートを設定するにはどうすればよいでしょうか? 質問する

2 つのビュー コントローラ間で通信するためのシンプルなデリゲートを設定するにはどうすればよいでしょうか? 質問する

2 つありUITableViewControllers、デリゲートを使用して子ビュー コントローラーから親に値を渡す必要があります。デリゲートとは何かはわかっているので、わかりやすい例を見たいだけです。

ありがとう

ベストアンサー1

簡単な例を次に示します。

子ビュー コントローラに がありUISlider、スライダーの値をデリゲート経由で親に渡すとします。

子ビュー コントローラのヘッダー ファイルで、デリゲート型とそのメソッドを宣言します。

子ビューコントローラ.h

#import <UIKit/UIKit.h>

// 1. Forward declaration of ChildViewControllerDelegate - this just declares
// that a ChildViewControllerDelegate type exists so that we can use it
// later.
@protocol ChildViewControllerDelegate;

// 2. Declaration of the view controller class, as usual
@interface ChildViewController : UIViewController

// Delegate properties should always be weak references
// See http://stackoverflow.com/a/4796131/263871 for the rationale
// (Tip: If you're not using ARC, use `assign` instead of `weak`)
@property (nonatomic, weak) id<ChildViewControllerDelegate> delegate;

// A simple IBAction method that I'll associate with a close button in
// the UI. We'll call the delegate's childViewController:didChooseValue: 
// method inside this handler.
- (IBAction)handleCloseButton:(id)sender;

@end

// 3. Definition of the delegate's interface
@protocol ChildViewControllerDelegate <NSObject>

- (void)childViewController:(ChildViewController*)viewController 
             didChooseValue:(CGFloat)value;

@end

子ビュー コントローラの実装では、必要に応じてデリゲート メソッドを呼び出します。

子ビューコントローラ.m

#import "ChildViewController.h"

@implementation ChildViewController

- (void)handleCloseButton:(id)sender {
    // Xcode will complain if we access a weak property more than 
    // once here, since it could in theory be nilled between accesses
    // leading to unpredictable results. So we'll start by taking
    // a local, strong reference to the delegate.
    id<ChildViewControllerDelegate> strongDelegate = self.delegate;

    // Our delegate method is optional, so we should 
    // check that the delegate implements it
    if ([strongDelegate respondsToSelector:@selector(childViewController:didChooseValue:)]) {
        [strongDelegate childViewController:self didChooseValue:self.slider.value];
    }
}

@end

親ビュー コントローラのヘッダー ファイルで、ChildViewControllerDelegateプロトコルを実装することを宣言します。

ルートビューコントローラ.h

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

@interface RootViewController : UITableViewController <ChildViewControllerDelegate>

@end

親ビューコントローラの実装では、デリゲートメソッドを適切に実装します。

ルートビューコントローラ.m

#import "RootViewController.h"

@implementation RootViewController

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ChildViewController *detailViewController = [[ChildViewController alloc] init];
    // Assign self as the delegate for the child view controller
    detailViewController.delegate = self;
    [self.navigationController pushViewController:detailViewController animated:YES];
}

// Implement the delegate methods for ChildViewControllerDelegate
- (void)childViewController:(ChildViewController *)viewController didChooseValue:(CGFloat)value {

    // Do something with value...

    // ...then dismiss the child view controller
    [self.navigationController popViewControllerAnimated:YES];
}

@end

おすすめ記事