尝试在ViewController上显示UIImagePickerController,其视图不在窗口层次结构中

时间:2018-05-22 19:24:30

标签: ios swift xcode

我遇到了麻烦,之后我从View Controller移动到第二个View Controller,然后切换回View Controller并点击它没有出现的UI Image Picker把我带回第二个View Controller。在它出现的调试器上:

  

尝试在窗口层次结构中显示其视图!

我真的变得疯了。我从第二个View Controller返回到第一个View Controller的代码位于一个按钮内,它位于下方:

public class AppDelegate : UIApplicationDelegate , IUNUserNotificationCenterDelegate, IMessagingDelegate 
{
    public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
    {
        App.Configure();

        if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
        {
            var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
            UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) => {
                Console.WriteLine(granted);
            });
        }
        else
        {
            var allNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
            var settings = UIUserNotificationSettings.GetSettingsForTypes(allNotificationTypes, null);
            UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
        }

        UIApplication.SharedApplication.RegisterForRemoteNotifications();

        UNUserNotificationCenter.Current.Delegate = this;
        Messaging.SharedInstance.Delegate = this;

        Messaging.SharedInstance.ShouldEstablishDirectChannel = true;

        Firebase.InstanceID.InstanceId.Notifications.ObserveTokenRefresh((sender, e) => {
            var newToken = Firebase.InstanceID.InstanceId.SharedInstance.Token;
            System.Diagnostics.Debug.WriteLine(newToken);
        });

        return true;
    }

    public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
    {
        Console.WriteLine("Registered for remote notifications");
        Console.WriteLine(deviceToken.GetBase64EncodedString(NSDataBase64EncodingOptions.None));
        Console.WriteLine(deviceToken);
    }

    [Export("messaging:didReceiveMessage:")]
    public void DidReceiveMessage(Messaging messaging, RemoteMessage remoteMessage)
    {
        Console.WriteLine("iOS 11 Foreground");
    }

   [Export("userNotificationCenter:DidReceiveRemoteNotification:withCompletionHandler:")]
    public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
    {
        Console.WriteLine("Received a notficaiton");
        completionHandler(UIBackgroundFetchResult.NewData);
    }

    [Export("userNotificationCenter:willPresent:withCompletionHandler:")]
    public void WillPresent(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
    {
        Console.WriteLine("Handling iOS 11 foreground notification");
        completionHandler(UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Alert);
    }

    [Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
    public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
    {
        completionHandler();
    }

    public void ApplicationReceivedRemoteMessage(RemoteMessage remoteMessage)
    {
    }

2 个答案:

答案 0 :(得分:0)

在第二个视图控制器中添加此变量:

var firstVC: ViewController?

和这个设置它的setter函数

setup(firstVC: ViewController) { 
   self.firstVC = firstVC 
} 

在第一个视图控制器中,当第二个视图控制器通过调用设置函数传递对self(第一个视图控制器)的引用时

let secondVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondViewController") as! ViewController
secondVC.setup(firstVC: self)
self.present(secondVC, animated: false, completion: nil)

现在当在secondVC中单击按钮时,

guard let first = self.firstVC else {return}
first.profileLink = linkProfilo
self.dismiss(animated: false, completion: nil)

答案 1 :(得分:0)

我终于找到了修复所有这些的方法:

@IBAction func profileButton(_ sender: Any) {
    if let webVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController") as? ViewController {
                let appDelegate = UIApplication.shared.delegate as! AppDelegate
                webVC.profileLink = linkProfilo
                dismiss(animated: false, completion: nil)
                appDelegate.window?.rootViewController!.present(webVC, animated: false, completion: nil)
            }
    }

足以添加appDelegate共享方法并在之前解散以呈现webVC以便以正确的方式共享数据,并且在呈现时不会导致第一个视图出现问题。

相关问题