Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从NavigationController切换到查看控制器抛出NSException终止_Ios_Segue_Uipageviewcontroller - Fatal编程技术网

Ios 从NavigationController切换到查看控制器抛出NSException终止

Ios 从NavigationController切换到查看控制器抛出NSException终止,ios,segue,uipageviewcontroller,Ios,Segue,Uipageviewcontroller,我正在尝试向视图控制器中的导航项添加一个按钮,该按钮将切换到PageViewController。我想这很简单,但当我点击按钮时,我收到以下错误: libc++abi.dylib: terminating with uncaught exception of type NSException XCode的编辑器显示: Thread 1: signal SIGABRT 以下是我的设置: 带导航栏按钮的ViewController: class AudioBookViewController:

我正在尝试向视图控制器中的导航项添加一个按钮,该按钮将切换到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)}
}
...
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{
    ...

}
}
自定义类PageViewController:

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)}
}
...
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{
    ...

}
}
下面是我的故事板的屏幕截图,显示了以下内容:

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)}
}
...
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是从AudiobooksViewController到PageViewController的,通过帮助按钮发送iAction

这是我第一次运行堆栈跟踪…希望我做得正确。我在segue代码之后运行了printstack语句

 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) 
0 KA有声读物0x00000001000102a8\u TFC13KA有声读物23有声读物视图控制器17帮助按钮按下DFP\u T+260
1 KA有声读物0x000000010001070c\u TToFC13KA有声读物23AudioBookViewController17帮助按钮按下P\u T\u72
2 UIKit 0x000000019792bd30+96
3 UIKit 0x0000000197a9f880+168
4 UIKit 0x000000019792bd30+96
5 UIKit 0x000000019792bcb0+80
6 UIKit 0x0000000197916128+452
7 UIKit 0x0000000197916290+812
8 UIKit 0x000000019792b59c+584
9 UIKit 0x000000019792b0c4+2484
10 UIKit 0x0000000197926328+2988
11 UIKit 0x00000001978f6da0+340
12 UIKit 0x00000001980e075c+2736
13 UIKit 0x00000001980da130+784
14核心基金会0x00000001919eeb5c+24
15 CoreFoundation 0x00000001919ee4a4+524
16 CoreFoundation 0x00000001919ec0a4+804
17 CoreFoundation 0x000000019191a2b8 CFRunLoopRunSpecific+444
18图形服务0x00000001933ce198 GSEventRunModal+180
19 UIKit 0x00000001979617fc+684
20 UIKit 0x000000019795c534 UIApplicationMain+208
21千卡有声读物0x000000010004f65c主+76
22 libdyld.dylib 0x00000001908fd5b8+4
libc++abi.dylib:以NSException类型的未捕获异常终止
(lldb)
更新:
我用普通的ViewController替换了PageViewController,segue工作正常。这确认问题出在我正在进入的PageViewController上。我认为PageViewController的第一屏VC没有被正确创建或引用。这是我第一次使用PageViewController,所以我肯定遗漏了什么…

我找到了解决方案。。。我无法完全以编程方式设置我的pageview控制器。相反,我必须为每个页面创建一个ViewController情节提要元素,同时命名与我的PageViewController中的标识符相对应的每个ViewController名称


确保已选中“使用情节提要ID”。

很难根据提供的内容进行诊断。您可以包括控制台中出现的所有内容吗?通常还有更多的信息(乍一看可能有些神秘/不重要,但通常有一些信息可以从中剔除)。如果没有这一点,堆栈跟踪也会很有用。您还可以添加一个异常断点,它有时可以帮助确定导致问题的确切代码行。