【问题标题】:Swift Delegate function not being executedSwift Delegate 函数未执行
【发布时间】:2016-04-19 16:24:44
【问题描述】:

我有一个包含三个 ViewController 的 UIPageViewcontroller。一个 Viewcontroller 是我的 ProfileViewcontroller。我的 ProfileViewController 中有一个按钮,按下时应该告诉 UIPageViewController 切换到下一个 Viewcontroller。 这是我第一次实现委托,但我无法弄清楚为什么它不起作用。

UIPageViewController 类:

class PageViewController: UIPageViewController, ProfileViewControllerDelegate {

private(set) lazy var orderedViewControllers: [UIViewController] = {


// The view controllers will be shown in this order
return [self.newColoredViewController("Search"),
      self.newColoredViewController("Menu"),
      self.newColoredViewController("Profile"),
      self.newColoredViewController("Offer")]
}()

override func viewDidLoad() {
super.viewDidLoad()

dataSource = self



if orderedViewControllers.count >= 2 {
scrollToViewController(orderedViewControllers[1])
}

}

// MARK: ProfileViewControllerDelegate
func profileViewControllerDidTouchOffer(viewController:ProfileViewController, sender: AnyObject) {
scrollToNextViewController()
print("I'm pressing the offer Button")
}

ProfileViewController 类:

protocol ProfileViewControllerDelegate : class {
func profileViewControllerDidTouchOffer(controller:   ProfileViewController, sender: AnyObject)
}

class ProfileViewController: UIViewController {

weak var delegate: ProfileViewControllerDelegate?

@IBOutlet var profileImageView: SpringImageView!
@IBOutlet var offerButton: UIButton!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func offerButtonTouchUpInside(sender: AnyObject) {

delegate?.profileViewControllerDidTouchOffer(self, sender: sender)

}

回答

我通过更改在 orderderedViewControllers 中添加 ViewControllers 的方式更新了 PageViewController 类:

private(set) lazy var orderedViewControllers: [UIViewController] = {
// The view controllers will be shown in this order

let profileVC = UIStoryboard(name: "Main", bundle: nil)   .instantiateViewControllerWithIdentifier("ProfileViewController") as!   ProfileViewController
profileVC.delegate = self

return [self.newColoredViewController("Search"),
      self.newColoredViewController("Menu"),
      profileVC,
      self.newColoredViewController("Offer")]
}()

【问题讨论】:

  • 看起来不错,我要寻找的显而易见的事情是当您启动 ProfileViewController 类时,您是否设置了它的委托。即你已经把所有的框架都放在了适当的位置,它可能就像你没有告诉 ProfileViewController 它的委托是 PageViewController 一样简单。
  • @TimBull 如何告诉 ProfileViewController 它的委托是 PageViewController?
  • 不使用 '?',您可以使用类似 ` if let del = delegate { del.profileViewControllerDidTouchOffer(self, sender: sender) } else { print("Invalid delegate ") }`

标签: ios swift delegates viewcontroller uipageviewcontroller


【解决方案1】:

看起来您正在使用 InterfaceBuilder,所以我不确定它是如何在那里完成的(可能在 prepareForSegue 中),但典型的模式是这样的:

let vc = ProfileViewController() 
vc.delegate = self // Or whatever you want to act as the delegate 
// Now show the view controller. 

关键是在使用视图控制器之前,你要确保它的委托属性被设置为委托并且符合协议。通常,调用控制器将是委托。

编辑:如果您使用的是 UIPageViewController,那么您应该将委托添加到 viewController,然后再将其传递给 setViewControllers https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPageViewControllerClassReferenceClassRef/#//apple_ref/occ/instm/UIPageViewController/setViewControllers:direction:animated:completion

【讨论】:

  • 问题是我使用了 UIPageViewController,所以没有使用 Segue 过渡到下一页。
  • @Mike - 但是,他的基本观点仍然有效。无论您在哪里初始化 ProfileViewController,都需要将其委托设置为正确的对象。
  • Mike - 我在答案中添加了一个编辑 - 只需在将 viewController 传递给 UIPageViewController 之前设置委托
  • 谢谢,这正是我所做的。更新我的问题以发布我的答案。
猜你喜欢
  • 2015-01-24
  • 2018-12-05
  • 2020-07-16
  • 2013-11-29
  • 2016-09-30
  • 2015-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多