从Appdelegate

时间:2015-07-29 15:58:52

标签: ios swift viewcontroller appdelegate

如果推送通知进入并且用户点击它,我通过以下代码加载特定的视图控制器:

let tabbarController = storyboard.instantiateViewControllerWithIdentifier("employerTabBarViewController") as! UITabBarController
tabbarController.selectedIndex = 0

self.window?.rootViewController = tabbarController
let navigationController = tabbarController.viewControllers?.first as!     UINavigationController
var applicantsVC = storyboard.instantiateViewControllerWithIdentifier("ApplicantsViewController") as! ApplicantsViewController
applicantsVC.job = job

navigationController.pushViewController(applicantsVC, animated: false)
self.window?.makeKeyAndVisible()

我的问题是:这段代码是否为每个点击的通知消息反复实例化ViewControllers,以便智能手机的内存越来越多地被使用?如果是这种情况,我可以在初始化新应用之前摆脱已经由应用程序的先前用法初始化的所有ViewControllers吗? popToRootViewController有帮助吗?

**更新** 我尝试了两个建议,但每次点击通知消息时,内存使用量增加了大约10 MB。也许它与我在AppDelegate中初始化ViewControllers的方法没有关联,但我加载“ApplicantsViewController”的方式或用let storyboard = UIStoryboard(name: "Main", bundle: nil)初始化Storyboard会导致这种影响吗?

这是ApplicantsViewController的初始化(我也取消注释reloadData()var tableViewController = UITableViewController() - 这并没有阻止内存增加):

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var openPositionsStatus: UILabel!
@IBOutlet weak var changeJobOfferStatusButton: UIBarButtonItem!

var job : JobOffer?
var favorites = [Employee]()
var applicants = [Employee]()
var applications = [Application]()
var takenPositions = 0
var refreshControl : UIRefreshControl?
var offeredJobsVC : OfferedJobsViewController?

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "receiveNotification:", name: "refreshApplicants", object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "receiveNotification:", name: "jobOfferStatusChanged", object: nil)
}


deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.dataSource = self
    tableView.delegate = self

    Helper.setBackground(self)

    refreshControl = UIRefreshControl()
    refreshControl!.addTarget(self, action: Selector("reloadData"), forControlEvents: UIControlEvents.ValueChanged)

    self.updateJobOfferActionButton(self, jobOffer: self.job!, button: self.changeJobOfferStatusButton)


    var tableViewController = UITableViewController()
    tableViewController.tableView = self.tableView
    tableViewController.refreshControl = refreshControl
    // call reloadData after refreshControl solved sorting problems
    reloadData()

}

2 个答案:

答案 0 :(得分:0)

是的,它会导致越来越多的ViewController堆叠在导航控制器中。我的选择是在appDelegate

中创建一个全局变量
var applicantsVC:ApplicantsViewController?

并且在通知功能中,请执行以下操作:

if (applicantsVC == nil){
     self.applicantsVC = storyboard.instantiateViewControllerWithIdentifier("ApplicantsViewController") as! ApplicantsViewController
}

答案 1 :(得分:0)

不,你不会在每次推送通知上分配更多内存,而是重新分配内存。

每次为UITabBarController实例创建rootViewController的新实例并将其设为UIWindow时:

let tabbarController = storyboard.instantiateViewControllerWithIdentifier("employerTabBarViewController") as! UITabBarController  
self.window?.rootViewController = tabbarController  

这意味着每次使用视图控制器的完整权限释放当前tabBarController并分配新的视图控制器。你很擅长这种方式。