如何设置一个简单的委托以在两个视图控制器之间进行通信? [英] How do I set up a simple delegate to communicate between two view controllers?

查看:126
本文介绍了如何设置一个简单的委托以在两个视图控制器之间进行通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个UITableViewController,需要使用委托将子视图控制器的值传递给父进程。我知道代表是什么,只是想看一个简单的例子。



谢谢

解决方案

简单示例...



我们假设子视图控制器有一个 UISlider 我们希望通过委托将滑块的值传递给父级。



在子视图控制器的头文件中,声明委托类型及其方法: / p>

ChildViewController.h

  #import < UIKit / UIKit.h> 

// 1.向前声明ChildViewControllerDelegate - 这只是声明
//存在一个ChildViewControllerDelegate类型,以便我们可以使用它
//稍后。
@protocol ChildViewControllerDelegate;

// 2.视图控制器类的声明,像往常一样
@interface ChildViewController:UIViewController

//委托属性应始终为弱引用
//参见http://stackoverflow.com/a/4796131/263871的理由
//(提示:如果您不使用ARC,请使用`assign`而不是`weak`)
@property(nonatomic,weak)id< ChildViewControllerDelegate>代表;

//一个简单的IBAction方法,我将与
// UI中的关闭按钮相关联。我们将在此处理程序中调用委托的childViewController:didChooseValue:
//方法。
- (IBAction)handleCloseButton:(id)sender;

@end

// 3.委托人界面的定义
@protocol ChildViewControllerDelegate< NSObject>

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

@end

在子视图控制器的实现中,调用委托方法



ChildViewController.m

  #importChildViewController.h

@implementation ChildViewController

- (void)handleCloseButton:(id)sender {
// Xcode会抱怨我们在这里访问一个超过
//的弱属性,因为理论上可以在访问之间填充
//导致不可预测的结果。所以我们先从
//本地,强烈地引用代表开始。
id< ChildViewControllerDelegate> strongDelegate = self.delegate;

//我们的委托方法是可选的,所以我们应该
//检查代理是否实现它
if([strongDelegate responsesToSelector:@selector(childViewController:didChooseValue :)] ){
[strongDelegate childViewController:self didChooseValue:self.slider.value];
}
}

@end

父视图控制器的头文件,声明它实现 ChildViewControllerDelegate 协议。



RootViewController.h

  #import< UIKit / UIKit.h> 
#importChildViewController.h

@interface RootViewController:UITableViewController< ChildViewControllerDelegate>

@end

在父视图控制器的实现中,实现委托方法



RootViewController.m

  #importRootViewController.h

@implementation RootViewController

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ChildViewController * detailViewController = [[ChildViewController alloc] init];
//将self指定为子视图控件的委托
detailViewController.delegate = self;
[self.navigationController pushViewController:detailViewController animated:YES];
}

//实现ChildViewControllerDelegate的委托方法
- (void)childViewController:(ChildViewController *)viewController didChooseValue:(CGFloat)value {

//做一些有价值的东西...

// ...然后关闭子视图控制器
[self.navigationController popViewControllerAnimated:YES];
}

@end

希望这有帮助! p>

I have two UITableViewControllers and need to pass the value from the child view controller to the parent using a delegate. I know what delegates are and just wanted to see a simple to follow example.

Thank You

解决方案

Simple example...

Let's say the child view controller has a UISlider and we want to pass the value of the slider back to the parent via a delegate.

In the child view controller's header file, declare the delegate type and its methods:

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

In the child view controller's implementation, call the delegate methods as required.

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

In the parent view controller's header file, declare that it implements the ChildViewControllerDelegate protocol.

RootViewController.h

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

@interface RootViewController : UITableViewController <ChildViewControllerDelegate>

@end

In the parent view controller's implementation, implement the delegate methods appropriately.

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

Hope this helps!

这篇关于如何设置一个简单的委托以在两个视图控制器之间进行通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆