Unexpected UIPageViewController behaviour

2020-07-13 09:14发布

问题:

I'm currently building a app using UIPageViewController. It is working as I expected until it reaches last page which contains numbers of UITextField.

But when I tapped one of instances of UITextField, it suddenly jumps to the previous page without any reason instead of showing a keyboard. When I scroll through to the last page and tap a text field again, it works well without any problem.

It is also fine when it shows last page immediately by tapping the navigation button that links to the last page and that is attached to the UIPageViewController instance.

This problem happens only when I scroll through pages from first to the last, and it happens only once at the first try.

I suspected low memory issues, so I set breakpoints in -didReceiveMemoryWarning method of both UIPageViewController instance and the last page view controller. But they were never called.

The second try is that I added a breakpoint in the last page view controller's -willMoveToParentViewController: method and set its condition to parent == nil(when it's removed from parent view controller, which is the UIPageViewController instance in question)

When I run the app, the breakpoint's call stack says that the method is called by UIPageViewController's -_flushViewController:animated:.

I don't know why this -flushViewController:animated: is called. If I can figure out the cause, it will help me with fixing the problem.

Is there anybody who can help me?

回答1:

I'm not sure what it is in your code that does this, but one probable approach in debugging this is to try and catch the UITextField's UIKeyboardWillShowNotification event to check if it's firing properly.

Also try to set a breakpoint at your page's viewWillAppear event to inspect if all of the UITextFields have been instantiated properly -- I would suspect that at the point where you tapped the first text field not all of them have instantiated yet, meaning that the object receiving the tap action will be your UIPageViewController instead of the UITextField.



回答2:

I found the answer here:

Cannot Impliment UITextview inside a UIPageViewController

Credit goes entirely to him.

I can confirm that it works. Here is what I did in my view controller:

//TextField
    UIScrollView *textFieldScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    [textField setText:@""];
    [textField setBackgroundColor:[UIColor whiteColor]];
    [textField.layer setCornerRadius:5.0f];
    [textField setDelegate:self];

    [textFieldScrollView addSubview:textField];
    [_someView addSubview:textFieldScroll];

Works like a charm. Let me know if you have any more questions.