我的协议没有调用
My Protocol is not Calling
我正在为调用方法使用协议,但我的方法没有call.Is有我使用的任何示例。
这是我的代码:
ViewController
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
extension ViewController :ViewController1Delegate
{
func hello()
{
println("hbgyguyg");
}
}
在视图控制器 1 中
import UIKit
@objc
protocol ViewController1Delegate
{
optional func hello()
}
class ViewController1: UIViewController {
var delegate: ViewController?
override func viewDidLoad() {
super.viewDidLoad()
delegate?.hello()
}
}
请帮助,我是 Swift.Any 的新手,我们将不胜感激。提前致谢
示例演示。
ViewController 文件
import UIKit
class ViewController: UIViewController, PopUpViewControllerDelegate
{
var popupVC: PopUpViewController!;
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view!.backgroundColor = UIColor.whiteColor();
self.popupVC = PopUpViewController();
self.popupVC.delegate = self;
self.showPopUpVC();
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func popUpViewControllerDidPressOK(popUpVC: PopUpViewController) {
println("Yay?");
self.closePopUpVC();
}
func showPopUpVC()
{
let delayTime = dispatch_time(DISPATCH_TIME_NOW,
Int64(1.0 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
self .presentViewController(self.popupVC, animated: true, completion: nil);
}
}
func closePopUpVC()
{
let delayTime = dispatch_time(DISPATCH_TIME_NOW,
Int64(1.0 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
self.dismissViewControllerAnimated(true, completion: nil);
}
}
}
弹出窗口ViewController 文件
import UIKit
protocol PopUpViewControllerDelegate
{
func popUpViewControllerDidPressOK(popUpVC: PopUpViewController);
}
class PopUpViewController: UIViewController {
var delegate: PopUpViewControllerDelegate!;
override func viewDidLoad() {
super.viewDidLoad()
self.view!.backgroundColor = UIColor.redColor();
// Do any additional setup after loading the view.
self.delegate!.popUpViewControllerDidPressOK(self);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
注意在我的 ViewController
viewDidLoad()
方法中,我有一行初始化弹出视图控制器,然后将其委托设置为 ViewController
本身:
self.popupVC = PopUpViewController();
self.popupVC.delegate = self; // you're missing this line I believe ?
我不使用 Interface Builder 或故事板,但可能 select 你的故事板中的 VC1 并查看连接检查器,看看你是否从 "delegate" 拖了一条线到你的 ViewController
文件所有者的事情。
最后,您应该会在 1 秒后看到一个红屏弹出窗口,然后 "Yay?" 一词登录到您的 Xcode 控制台,最后弹出 VC 消失。
我正在为调用方法使用协议,但我的方法没有call.Is有我使用的任何示例。
这是我的代码:
ViewController
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
extension ViewController :ViewController1Delegate
{
func hello()
{
println("hbgyguyg");
}
}
在视图控制器 1 中
import UIKit
@objc
protocol ViewController1Delegate
{
optional func hello()
}
class ViewController1: UIViewController {
var delegate: ViewController?
override func viewDidLoad() {
super.viewDidLoad()
delegate?.hello()
}
}
请帮助,我是 Swift.Any 的新手,我们将不胜感激。提前致谢
示例演示。
ViewController 文件
import UIKit
class ViewController: UIViewController, PopUpViewControllerDelegate
{
var popupVC: PopUpViewController!;
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view!.backgroundColor = UIColor.whiteColor();
self.popupVC = PopUpViewController();
self.popupVC.delegate = self;
self.showPopUpVC();
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func popUpViewControllerDidPressOK(popUpVC: PopUpViewController) {
println("Yay?");
self.closePopUpVC();
}
func showPopUpVC()
{
let delayTime = dispatch_time(DISPATCH_TIME_NOW,
Int64(1.0 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
self .presentViewController(self.popupVC, animated: true, completion: nil);
}
}
func closePopUpVC()
{
let delayTime = dispatch_time(DISPATCH_TIME_NOW,
Int64(1.0 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
self.dismissViewControllerAnimated(true, completion: nil);
}
}
}
弹出窗口ViewController 文件
import UIKit
protocol PopUpViewControllerDelegate
{
func popUpViewControllerDidPressOK(popUpVC: PopUpViewController);
}
class PopUpViewController: UIViewController {
var delegate: PopUpViewControllerDelegate!;
override func viewDidLoad() {
super.viewDidLoad()
self.view!.backgroundColor = UIColor.redColor();
// Do any additional setup after loading the view.
self.delegate!.popUpViewControllerDidPressOK(self);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
注意在我的 ViewController
viewDidLoad()
方法中,我有一行初始化弹出视图控制器,然后将其委托设置为 ViewController
本身:
self.popupVC = PopUpViewController();
self.popupVC.delegate = self; // you're missing this line I believe ?
我不使用 Interface Builder 或故事板,但可能 select 你的故事板中的 VC1 并查看连接检查器,看看你是否从 "delegate" 拖了一条线到你的 ViewController
文件所有者的事情。
最后,您应该会在 1 秒后看到一个红屏弹出窗口,然后 "Yay?" 一词登录到您的 Xcode 控制台,最后弹出 VC 消失。