如何设置一个简单的委托来在两个视图控制器之间进行通信?

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

谢谢

简单的例子…

比方说,子视图控制器有一个UISlider ,我们想通过UISlider把滑块的值传递给父代。

在子视图控制器的头文件中,声明委托types及其方法:

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 

在子视图控制器的实现中,根据需要调用委托方法。

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 

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

RootViewController.h

 #import <UIKit/UIKit.h> #import "ChildViewController.h" @interface RootViewController : UITableViewController <ChildViewControllerDelegate> @end 

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

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 

希望这可以帮助!

这下面的代码只是显示委托概念的最基本的用法..你根据你的要求命名variables和类。

首先你需要声明一个协议:

我们称之为MyFirstControllerDelegate.h

 @protocol MyFirstControllerDelegate - (void) FunctionOne: (MyDataOne*) dataOne; - (void) FunctionTwo: (MyDatatwo*) dataTwo; @end 

导入MyFirstControllerDelegate.h文件并使用协议MyFirstControllerDelegate确认您的FirstController

 #import "MyFirstControllerDelegate.h" @interface FirstController : UIViewController<MyFirstControllerDelegate> { } @end 

在实现文件中,你需要实现协议的两个function:

 @implementation FirstController - (void) FunctionOne: (MyDataOne*) dataOne { //Put your finction code here } - (void) FunctionTwo: (MyDatatwo*) dataTwo { //Put your finction code here } //Call below function from your code -(void) CreateSecondController { SecondController *mySecondController = [SecondController alloc] initWithSomeData:.]; //..... push second controller into navigation stack mySecondController.delegate = self ; [mySecondController release]; } @end 

在你的SecondController中

 @interface SecondController:<UIViewController> { id <MyFirstControllerDelegate> delegate; } @property (nonatomic,assign) id <MyFirstControllerDelegate> delegate; @end 

SecondController的实现文件中。

 @implementation SecondController @synthesize delegate; //Call below two function on self. -(void) SendOneDataToFirstController { [delegate FunctionOne:myDataOne]; } -(void) SendSecondDataToFirstController { [delegate FunctionTwo:myDataSecond]; } @end 

这是关于委托的维基文章。

您需要使用委托和协议。 这是一个网站的例子http://iosdevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html