私はViewController "LandingPageViewController"を持っています。これには組み込みのPageViewController "pageViewController"があります。デリゲート返すViewControllerのインスタンス番号が正しくありません
"pageViewController"から "LandingPageViewControlelr"に書き込むデリゲート関数を作成しました。これはうまく動作し、データは "LandingPageController"に送られますが、デリゲートが新しいインスタンスを作成しているので、 。
アプリは最初、それは "LandingPageViewController" に行くと、私はのviewDidLoadとviewDidAppearで(自己)を印刷する場合、これが結果です起動:
Delegate viewDidLoad...<ParseStarterProject_Swift.LandingPageViewController: 0x7fbf29406e60>
Delegate viewDidAppear...<ParseStarterProject_Swift.LandingPageViewController: 0x7fbf29406e60>
私は3ページ目にオーバースクロールしていると、デリゲートは、これが受信している私の「LandingPageViewController」からコードである(自己)
Delegate function...<ParseStarterProject_Swift.LandingPageViewController: 0x7fbf2940e3a0>
私は印刷の次の結果を得ると呼ばれるその後いったんデータ:
class pageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
let myHeight = 667
let myCenter = 333.5
// define the delegate for use
var indexDelegate: GetMyIndexDelegate?
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if (completed && finished) {
if let currentVC = pageViewController.viewControllers?.last {
let index = vcArr.index(of: currentVC)
if index == 3 {
// this is the line causing problem
self.indexDelegate = LandingPageViewController()
print("PageView Index Defined As...\(self.indexDelegate)")
indexDelegate?.getIndex(index: index!)
//goToVC()
}
}
}
}
:
protocol GetMyIndexDelegate {
func getIndex(index: Int)
}
class LandingPageViewController: UIViewController, GetMyIndexDelegate {
@IBOutlet var createAccountButton: UIButton!
//delegate functionto get index
func getIndex(index: Int) {
if index == 3 {
print("Delegate function...\(self)")
//print(createAccountButton)
}
}
override func viewDidLoad() {
super.viewDidLoad()
print("Delegate viewDidLoad...\(self)")
}
}
、ここでは "pageViewController" クラスからのコードです
私は、これは問題がここにあると考えている:それは間違ったインスタンスを定義している
self.indexDelegate = LandingPageViewController()
?????
サイドヒントとして、viewControllerから「pageViewController」の最初のページに向かう別のデリゲートメソッドがありました。そこには、ページ内のページのインデックスを正しく定義しなければならないという問題がありました
self.delegate = self.childViewControllers[0] as! pageViewController
LandingPageViewControllerが親であるとして、これは上記の質問にどのように動作するかわからないイム:次のコードでUIPageViewController? pageViewControllerの?
プログラミングスタイルの観点からは、 'pageViewController'クラス(または大文字で始まるもの)に' pageViewController'クラスの名前を変更することをお勧めします。クラスと型は常に大文字で始める必要があります。これは必須ではありませんが、コードを読みやすくするためには大事なことです... – Rob
はしばらく私を悩ませていましたが、そのリストには – DuckMan