我的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
)之间的距离。如果没有这个约束,该组件将被工具栏遮盖。
let rootTabBarController = self.window?.rootViewController as! UITabBarController
。 - SagittariusA