3

私のアプリiOSのメインスケルトンはTabBarControllerで動作します。とにかく、通知が届くと余分な動作があります。私のアプリはプッシュ通知を受信したときに、次の処理を行います。Swift:AppDelegateから起動されたビューコントローラとナビゲーションコントローラを閉じる方法

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { 
    //application.applicationIconBadgeNumber = 0 
    //application.cancelAllLocalNotifications() 
    let storyboard = UIStoryboard(name: "Main", bundle: nil) 
    let notificationController = storyboard.instantiateViewControllerWithIdentifier("DynamicEventsViewController") as! DynamicEventsViewController 
    notificationController.isLoadedFromNotification = true 
    notificationController.eventTitle = userInfo["aps"]!["alert"] as! String 
    notificationController.eventDescription = userInfo["aps"]!["message"] as! String 
    let navigationController = UINavigationController() 
    navigationController.pushViewController(notificationController, animated: true) 
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds) 
    window!.rootViewController = navigationController 
    self.window?.makeKeyAndVisible() 

} 

、これは相対的なインスタンス化ビューコントローラのコードです:私は、ビューを「Chiudi」ボタンをタップするととにかく

class DynamicEventsViewController:UIViewController { 

@IBOutlet weak var upDistanceConstraint: NSLayoutConstraint! 
@IBOutlet weak var dynamicEventTitle:UITextField! 
@IBOutlet weak var dynamicEventDescription:UITextView! 

var eventTitle:String? = nil 
var eventDescription:String? = nil 

var isLoadedFromNotification = false 


override func viewDidLoad() { 
    super.viewDidLoad() 

     self.navigationItem.titleView = UIImageView(image: UIImage(named: "icons/bar.png")) 
     self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor() 

     if (self.navigationItem.leftBarButtonItem == nil) { 
      let leftButton = UIBarButtonItem(title: "Chiudi", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(DynamicEventsViewController.back(_:))) 
      self.navigationItem.leftBarButtonItem = leftButton 
     } 

    if (self.eventTitle != nil && self.eventDescription != nil) { 
     self.dynamicEventTitle.text = self.eventTitle?.uppercaseString 
     self.dynamicEventDescription.text = self.eventDescription 
    } 
} 

func back(sender: UIBarButtonItem) { 
    self.navigationController?.popViewControllerAnimated(true) 
} 

} 

私がこのアプリをTabBarControllerに戻したいと思っている間に、コントローラが閉じることはありません。どちらが正しい方法ですか? didReceiveLocal(Remote)Notification()AppDelegate.swiftから

最終溶液

私が実行:

let storyboard = UIStoryboard(name: "Main", bundle: nil) 
    let notificationController = storyboard.instantiateViewControllerWithIdentifier("DynamicEventsViewController") as! DynamicEventsViewController 
    notificationController.isLoadedFromNotification = true 
    notificationController.eventTitle = userInfo["aps"]!["alert"] as? String 
    notificationController.eventDescription = userInfo["aps"]!["message"] as? String 
    notificationController.isLoadedFromNotification = true 

    if let tabBarController = self.window?.rootViewController { 
     tabBarController.presentViewController(notificationController, animated: true, completion: nil) 
    } 

私のビューコントローラに私が実行している間:

self.upDistanceConstraintの間の距離を示す NSLayoutConstraintある
if (isLoadedFromNotification) { 
     self.upDistanceConstraint.constant = 90 

     let navigationBar:UINavigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 80)) 
     navigationBar.backgroundColor = UIColor.whiteColor() 

     let navigationItem:UINavigationItem = UINavigationItem() 

     let leftButton:UIBarButtonItem = UIBarButtonItem(title: "Chiudi", style: .Plain, target: self, action: #selector(DynamicEventsViewController.exit(_:))) 

     navigationItem.titleView = UIImageView(image: UIImage(named: "icons/bar.png")) 
     navigationItem.leftBarButtonItem = leftButton 
     self.navigationItem.titleView = UIImageView(image: UIImage(named: "icons/bar.png")) 

     navigationBar.items = [navigationItem] 

     self.view.addSubview(navigationBar) 

} 

をバーと最初のw私の場合はUITextFieldだった私のビューコントローラ内のidgetは、そうでなければ、バーによって隠されていたでしょう。

答えて

2

あなたが通知を受信すると、あなたのTabBarControllerはすでにあなたのアプリケーションのrootViewControllerです:あなたはナビゲーションコントローラにあなたのnotificationControllerを埋め込む必要はありませんし、次のことで、あなたのAppDelegate内のコードを置き換えることができます。

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { 
    let storyboard = UIStoryboard(name: "Main", bundle: nil) 
    let notificationController = storyboard.instantiateViewControllerWithIdentifier("DynamicEventsViewController") as! DynamicEventsViewController 
    notificationController.isLoadedFromNotification = true 
    notificationController.eventTitle = userInfo["aps"]!["alert"] as! String 
    notificationController.eventDescription = userInfo["aps"]!["message"] as! String 

    if let tabBarController = self.window?.rootViewController { 
     tabBarController.presentViewController(notificationController, animated: true, completion: nil) 
    } 

} 

そして、あなたは(あなたのストーリーボードに)あなたのDynamicEventsViewControllerにカスタムナビゲーションバーを追加する必要がありますので、のようなあなたのクラスにリンク:

@IBOutlet weak var myNavigationBar: UINavigationBar 

は、最後にあなたがあなたのバックボタンのイベントハンドラを交換する必要がありますフォローインg:

func back(sender: UIBarButtonItem) { 
     self.dismissViewControllerAnimated(true, completion: nil) 
} 
1

ナビゲーションコントローラを作成できますが、ルートコントローラを置き換えることはできません。ちょうどあなたのタブバーコントローラからアニメーションなしでそれを提示してください。あなたは、通知からナビゲーションコントローラを非表示にする必要がある場合は、ちょうどそれを却下:

func showNavigation(){ 
    tabBarController.presentViewController(navController, animated: false) { 
    } 
} 

func hideNavigation() { 
    tabBarController.dismissViewControllerAnimated(true) { 
    } 
} 

tabBarControllerがあなたのウィンドウです.rootViewController

更新

代わりの

self.window = UIWindow(frame: UIScreen.mainScreen().bounds) 
window!.rootViewController = navigationController 
self.window?.makeKeyAndVisible() 

これを試してみてください?

rootTabBarController.presentViewController(navigationController, animated: true) {} 
+0

はいです。私はすぐにしよう!それが私のアプリケーションデリゲートでやっていることです: 'let rootTabBarController = self.window?.rootViewController as! UITabBarController' –

+0

@LoryLory答えを –

+0

に更新しましたが、これは私が書いたものですが動作しません... –

関連する問題