从iPhone更改为通用后iOS应用程序崩溃:

时间:2015-11-03 17:32:56

标签: ios objective-c ipad uialertcontroller

我从xCode收到错误说明:

2015-11-03 12:25:04.833 New Application[1122:1104042] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Your application has presented a UIAlertController (<UIAlertController: 0x14f68cb50>) of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem.  If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.'
*** First throw call stack:
(0x182e1cf5c 0x19799ff80 0x188c50068 0x1886c0254 0x1886be384 0x18861c844 0x188628de4 0x1883651e4 0x182dd3c30 0x182dd19d4 0x182dd1e04 0x182d00dc0 0x18dca8088 0x1883daf60 0x1000f0d00 0x1981be8b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

就行:

class AppDelegate: UIResponder, UIApplicationDelegate {

在:

//
//  AppDelegate.swift
//  FlappyBird
//
//  Created by Nate Murray on 6/2/14.
//  Copyright (c) 2014 Fullstack.io. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

在这里找到:

我的来源: https://github.com/androiddeveloperfl/SADiver

原始来源:

https://github.com/fullstackio/FlappySwift/blob/master/FlappyBird/AppDelegate.swift

非常感谢任何建议/答案/解决方案。

2 个答案:

答案 0 :(得分:0)

当你在iPad上召唤一个动作表时,它就是一个弹出窗口。这意味着您必须说明弹出箭头指向的位置。获取警报的弹出式演示控制器,并在显示警报时向其提供此信息。

以下是一个例子:

    let alert = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
    func handler(act:UIAlertAction!) {
        print("User tapped \(act.title)")
    }
    alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: handler))
    alert.addAction(UIAlertAction(title: "Hey", style: .Default, handler: handler))
    alert.addAction(UIAlertAction(title: "Ho", style: .Default, handler: handler))
    alert.addAction(UIAlertAction(title: "Hey Nonny No", style: .Default, handler: handler))
    self.presentViewController(alert, animated: true, completion: nil)
    // if we do no more than that, we'll crash on iPad
    // here's how to fix that:
    if let pop = alert.popoverPresentationController {
        let b = sender as! UIBarButtonItem
        pop.barButtonItem = b // or set sourceView and sourceRect
    }

答案 1 :(得分:0)

当您尝试在SplashViewController中展示操作表时,应用会崩溃。在iPad上,操作表有一个小箭头指向触发弹出窗口的控件或视图,您需要告诉它在哪里指向。在您的代码中,您可以在alert(title: String, message: String)函数中执行类似的操作;

    alertController.popoverPresentationController?.sourceView = self.view
    alertController.popoverPresentationController?.sourceRect = CGRectMake(self.view.bounds.size.width / 2.0, self.view.bounds.size.height / 2.0, 1.0, 1.0)

将箭头放在视图的中心。这可能看起来很奇怪,所以您可能还想将UIAlertController的样式从操作表更改为警报:

    let alertController = UIAlertController(title: title, message:  message, preferredStyle: .Alert)

对我来说看起来更好。然后,您不需要关于popoverPresentationController

的行