#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
import UIKit
//Declare the Protocol into your SecondVC
protocol DataDelegate {
func sendData(data : String)
}
class ViewControllerB : UIViewController {
//Declare the delegate property in your SecondVC
var delegate : DataDelegate?
var data : String = "Send data to ViewControllerA."
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func btnSendDataPushed(_ sender: UIButton) {
// Call the delegate method from SecondVC
self.delegate?.sendData(data:self.data)
dismiss(animated: true, completion: nil)
}
}
ViewControllerA确认协议并预期通过委托方法sendData接收数据
import UIKit
// Conform the DataDelegate protocol in ViewControllerA
class ViewControllerA : UIViewController , DataDelegate {
@IBOutlet weak var dataLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func presentToChild(_ sender: UIButton) {
let childVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier:"ViewControllerB") as! ViewControllerB
//Registered delegate
childVC.delegate = self
self.present(childVC, animated: true, completion: nil)
}
// Implement the delegate method in ViewControllerA
func sendData(data : String) {
if data != "" {
self.dataLabel.text = data
}
}
}
4条答案
按热度按时间mrwjdhj31#
下面是一个简单的例子:
假设子视图控制器有一个
UISlider
,我们想通过一个代理将滑块的值传递回父视图控制器。在子视图控制器的头文件中,声明委托类型及其方法:
在子视图控制器的实现中,根据需要调用委托方法。
在父视图控制器的头文件中,声明它实现
ChildViewControllerDelegate
协议。在父视图控制器的实现中,适当地实现委托方法。
az31mfrm2#
下面的代码只是展示了委托概念的基本用法。你可以根据自己的需要命名变量和类。
首先,您需要声明一个协议:
我们将其命名为我的第一个控制器委托。h
导入MyFirstControllerDelegate.h文件并使用协议MyFirstControllerDelegate确认您的第一控制器
在实现文件中,需要实现协议的两个函数:
在您的第二控制器中:
在SecondController的实现文件中。
这是一篇关于委派的维基文章。
iyr7buue3#
下面的解决方案是使用delegate将数据从VC2发送到VC1的非常基本和简单的方法。
PS:此解决方案采用Xcode 9.X和Swift 4
已声明协议并在ViewControllerB中创建了委托变量
ViewControllerA确认协议并预期通过委托方法sendData接收数据
ctehm74n4#
您需要使用委托和协议。下面是一个包含http://iosdevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html示例的站点