我的应用iOS主骨架适用于TabBarController
。无论如何,当通知到达时会有额外的行为。收到推送通知后,我的应用会执行以下操作:
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()
}
这是相对实例化视图控制器的代码:
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)
}
}
无论如何,如果我点击“Chiudi”按钮,视频控制器不会关闭,而我希望应用程序返回TabBarController
。哪种方法正确?
最终解决方案
来自AppDelegate.swift
didReceiveLocal(Remote)Notification()
我执行的内容:
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)
}
在我的视图控制器中执行:
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)
}
其中self.upDistanceConstraint
是NSLayoutConstraint
,表示条形图与我视图控制器中的第一个小部件之间的距离,在我的情况下,它是UITextField
,否则它将被隐藏吧。
答案 0 :(得分: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(在Storyboard中)添加自定义导航栏,并将其链接到您的类,如下所示:
@IBOutlet weak var myNavigationBar: UINavigationBar
最后,您需要使用以下内容替换后退按钮事件处理程序:
func back(sender: UIBarButtonItem) {
self.dismissViewControllerAnimated(true, completion: nil)
}
答案 1 :(得分: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) {}