从NavigationController看到控制器抛出NSException终止

时间:2017-05-01 23:34:01

标签: ios segue uipageviewcontroller

我正在尝试向我的视图控制器中的导航项添加一个BarButton,它将转换为PageViewController。我认为这将非常简单,但当我点击按钮时,我收到此错误:

libc++abi.dylib: terminating with uncaught exception of type NSException

XCode的编辑显示:

Thread 1: signal SIGABRT

这是我的设置:

带导航栏按钮的ViewController:

class AudioBookViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UINavigationBarDelegate{

@IBAction func helpButtonPressed(_ sender: Any) {
    performSegue(withIdentifier: Constants.Segues.SegueFromAudiobooksToHelp, sender: self)

    //The exception is thrown when trying to perform the segue above

    Thread.callStackSymbols.forEach{print("    ",$0)}
}
...

自定义类PageViewController:

class PageViewController: UIPageViewController,UIPageViewControllerDelegate, UIPageViewControllerDataSource {

let screens = ["Screen1","Screen2"]

override func viewDidLoad() {
    super.viewDidLoad()
    self.delegate = self as UIPageViewControllerDelegate
    self.dataSource = self as UIPageViewControllerDataSource
}

override func viewDidAppear(_ animated: Bool) {
    let vc = self.storyboard?.instantiateViewController(withIdentifier: screens[0])
    setViewControllers([vc!], // Has to be a single item array, unless you're doing double sided stuff I believe
        direction: .forward,
        animated: true,
        completion: nil)
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
    if let identifier = viewController.restorationIdentifier, let index = screens.index(of: identifier){
        if index > 0{
            return self.storyboard?.instantiateViewController(withIdentifier: screens[index - 1])
        }
    }

    return nil
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
    if let identifier = viewController.restorationIdentifier, let index = screens.index(of: identifier){
        if index < screens.count - 1 {
            return self.storyboard?.instantiateViewController(withIdentifier: screens[index + 1])
        }
    }

    return nil
}

func presentationCount(for pageViewController: UIPageViewController) -> Int {
    return screens.count
}

func presentationIndex(for pageViewController: UIPageViewController) -> Int {
    if let identifier = viewControllers?.first?.restorationIdentifier, let index = screens.index(of: identifier){
            return index
    }
    return 0
}

}

我的常数枚举:

struct Constants {

struct NotificationKeys{
    ...

}

struct Segues {
    ....
    static let SegueFromAudiobooksToHelp = "SegueFromAudiobooksToHelp"
    static let SegueFromTopicsToHelp = "SegueFromTopicsToHelp"
    ....
}

struct RegistrationOutcome{
    ...

}
}

以下是我的故事板显示segue的屏幕截图:

Segue是从AudiobooksViewController到PageViewController,从帮助按钮发送IBAction Segue from Audiobooks VC to Help VC

这是我第一次运行堆栈跟踪... 希望我正确地执行此操作。我在segue代码之后立即运行了打印堆栈语句。

 0   KA Audio Book                       0x00000001000102a8 _TFC13KA_Audio_Book23AudioBookViewController17helpButtonPressedfP_T_ + 260
 1   KA Audio Book                       0x000000010001070c _TToFC13KA_Audio_Book23AudioBookViewController17helpButtonPressedfP_T_ + 72
 2   UIKit                               0x000000019792bd30 <redacted> + 96
 3   UIKit                               0x0000000197a9f880 <redacted> + 168
 4   UIKit                               0x000000019792bd30 <redacted> + 96
 5   UIKit                               0x000000019792bcb0 <redacted> + 80
 6   UIKit                               0x0000000197916128 <redacted> + 452
 7   UIKit                               0x0000000197916290 <redacted> + 812
 8   UIKit                               0x000000019792b59c <redacted> + 584
 9   UIKit                               0x000000019792b0c4 <redacted> + 2484
 10  UIKit                               0x0000000197926328 <redacted> + 2988
 11  UIKit                               0x00000001978f6da0 <redacted> + 340
 12  UIKit                               0x00000001980e075c <redacted> + 2736
 13  UIKit                               0x00000001980da130 <redacted> + 784
 14  CoreFoundation                      0x00000001919eeb5c <redacted> + 24
 15  CoreFoundation                      0x00000001919ee4a4 <redacted> + 524
 16  CoreFoundation                      0x00000001919ec0a4 <redacted> + 804
 17  CoreFoundation                      0x000000019191a2b8 CFRunLoopRunSpecific + 444
 18  GraphicsServices                    0x00000001933ce198 GSEventRunModal + 180
 19  UIKit                               0x00000001979617fc <redacted> + 684
 20  UIKit                               0x000000019795c534 UIApplicationMain + 208
 21  KA Audio Book                       0x000000010004f65c main + 76
 22  libdyld.dylib                       0x00000001908fd5b8 <redacted> + 4
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

更新 我用常规的ViewController替换了PageViewController,而segue工作得很好。这证实问题在于我正在进行的PageViewController。我相信PageViewController的第一个屏幕VC没有被正确创建或引用。这是我第一次使用PageViewController,所以我必须遗漏一些东西......

1 个答案:

答案 0 :(得分:0)

我已经达成了解决方案......我无法完全以编程方式设置我的网页浏览控制器。相反,我必须为每个页面创建一个ViewController Storyboard元素,同时命名与PageViewController中的标识符相对应的每个ViewController名称。

确保选中&#39;使用故事板ID&#39;。