У меня есть UIPageViewcontroller, содержащий три ViewControllers. Один Viewcontroller - это мой ProfileViewcontroller. У меня есть кнопка в моем ProfileViewController, которая должна указывать UIPageViewCongtroller при нажатии, чтобы перейти к следующему Viewcontroller. Это моя первая реализация делегата, но я просто не могу понять, почему он не работает.Функция Swift Delegate не выполняется
класс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)
}
Ответ
Я обновил класс PageViewController, изменяя способ, которым я добавить ViewControllers в orderderedViewControllers:
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? '?' –
Вместо использования, вы можете поймать некоторые логические ошибки с рисунком, как '\t \t, если пусть дель = делегат { \t \t \t del.profileViewControllerDidTouchOffer (самость, отправитель: отправитель) \t \t} еще { \t \t \t print («Недопустимый делегат») \t} ' –