ios - 如何设置一个简单的委托(delegate)来在两个 View Controller 之间

我有两个 UITableViewControllers 并且需要使用委托(delegate)将值从 subview Controller 传递给父 View 。我知道委托(delegate)是什么,只是想看一个简单易学的例子。

谢谢

最佳答案

简单的例子...

假设 subview Controller 有一个 UISlider,我们想通过委托(delegate)将 slider 的值传回父 View 。

在 subview Controller 的头文件中,声明委托(delegate)类型及其方法:

ChildViewController.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

在 subview Controller 的实现中,根据需要调用委托(delegate)方法。

ChildViewController.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

在父 View Controller 的头文件中,声明它实现了ChildViewControllerDelegate协议(protocol)。

RootViewController.h

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

@interface RootViewController : UITableViewController <ChildViewControllerDelegate>

@end

在父 View Controller 的实现中,适本地实现委托(delegate)方法。

RootViewController.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

希望这会有所帮助!

关于ios - 如何设置一个简单的委托(delegate)来在两个 View Controller 之间进行通信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6168919/

相关文章:

objective-c - 为什么在 C 中将 1,000,000,000 写为 1000*1000

iphone - 错误 : The service is invalid

ios - 如何在 UITableView 中设置分隔符的全宽

ios - UIView 隐藏/显示动画

ios - 使用 glob 获取目录中的文件列表

ios - 无法同时满足约束,将尝试通过打破约束来恢复

ios - iPhone SDK : what is the difference between

ios - 以编程方式在 UIStackView 中添加 View

ios - Objective-C 和 Swift URL 编码

objective-c - 关于iOS5 SDK中自动引用计数的一些问题