简体   繁体   English

Swift Delegate函数未被执行

[英]Swift Delegate function not being executed

I have a UIPageViewcontroller containing three ViewControllers. 我有一个包含三个ViewControllers的UIPageViewcontroller。 One Viewcontroller is my ProfileViewcontroller. 一个Viewcontroller是我的ProfileViewcontroller。 I have a button in my ProfileViewController, which should tell the UIPageViewCongtroller when pressed, to switch to the next Viewcontroller. 我的ProfileViewController中有一个按钮,按下时应告诉UIPageViewCongtroller,切换到下一个Viewcontroller。 It's my first time implementing a delegate, but I just can't figure out why its not working. 这是我第一次实施代表,但我无法弄清楚为什么它不起作用。

UIPageViewController class: 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 class: 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)

}

Answer 回答

I updated the PageViewController class by changing the way I add the ViewControllers in orderderedViewControllers: 我通过更改在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")]
}()

Looks like you're using the InterfaceBuilder so I'm not sure how it's done there (probably in prepareForSegue), but a typical pattern is something like this: 看起来你正在使用InterfaceBuilder,所以我不确定它是如何在那里完成的(可能在prepareForSegue中),但典型的模式是这样的:

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

The key is that before using the view controller, you make sure it's delegate property is set to the thing that is the delegate and conforms to the protocol. 关键是在使用视图控制器之前,确保它的委托属性设置为委托的东西并符合协议。 Typically the calling controller would be the delegate. 通常,呼叫控制器将是代表。

EDIT: If you're using the UIPageViewController, then you should add the delegate to the viewController before passing it to setViewControllers https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPageViewControllerClassReferenceClassRef/#//apple_ref/occ/instm/UIPageViewController/setViewControllers:direction:animated:completion : 编辑:如果您正在使用UIPageViewController,那么您应该将该委托添加到viewController,然后再将其传递给setViewControllers https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPageViewControllerClassReferenceClassRef/#//apple_ref / occ / instm / UIPageViewController / setViewControllers:direction:animated:completion

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM