将导航堆栈的值存储在字典iOS中

时间:2019-05-15 09:02:18

标签: ios swift uiviewcontroller uinavigationcontroller nsmutabledictionary

每次用户访问我的应用程序中的任何屏幕时,我都需要跟踪用户的导航流程并将其存储在Dictionary中。通过使用代码

,我可以在控制台输出中看到导航堆栈

navigationController?.viewControllers

但是不知道如何将其存储在字典中。

请帮助

3 个答案:

答案 0 :(得分:0)

您可以使用{p>来获取UIViewControllers上所有navigationStack的名称,

let controllers = self.navigationController?.viewControllers.compactMap({ String(describing: type(of: $0.self)) }).joined(separator: "_")
print(controllers) //Output: "VC1_VC2_VC3"

您可以将controllers字符串存储在文件中。

答案 1 :(得分:0)

希望这有助于获取ViewController。遵循此步骤并使用单例将其存储到全局词典中。

全局功能:

public func storeCurrentViewController(_ currentFile: String = #file)
{
    let className = currentFile.components(separatedBy: "/").last ?? ""
    let classNameArr = className.components(separatedBy: ".")
    print("\n\n--> CurrentViewController:  \(classNameArr[0])")

    //*********** Use dictionary and do your logics

}

函数调用:

override func viewDidLoad() {
    super.viewDidLoad()
    storeCurrentViewController()
}

答案 2 :(得分:0)

NSArray *viewControllers = [[self navigationController] viewControllers];
                for( int i=0;i<[viewControllers count];i++){
                    id obj=[viewControllers objectAtIndex:i];    
                    NSLog(@"%@",[obj classNameForClass:[obj class]]);
}

不确定是否可行。试试看...