Ios 从自定义UIViewController获取自定义NavigationController类
我正在尝试在iOS应用程序中实现侧导航菜单(Android中的一个菜单)。到目前为止,我尝试了以下方法:- AppDelegate.swift:-Ios 从自定义UIViewController获取自定义NavigationController类,ios,swift,storyboard,Ios,Swift,Storyboard,我正在尝试在iOS应用程序中实现侧导航菜单(Android中的一个菜单)。到目前为止,我尝试了以下方法:- AppDelegate.swift:- class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var bridge: RCTBridge! func application(_ application: UIApplication, didFinishLaunchingW
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var bridge: RCTBridge!
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let sb = UIStoryboard(name: "mystoryboard", bundle: Bundle.main)
let rootViewController: UIViewController =
sb.instantiateViewController(withIdentifier:
"NativeLabelSwiftViewController") as UIViewController
let navigationController = UINavigationController(rootViewController:
rootViewController)
window = UIWindow(frame: UIScreen.main.bounds)
window!.rootViewController = navigationController
window!.makeKeyAndVisible()
return true
}
上面是我的AppDelegate类,因为它是一个React本机项目,我们也有生成的代码。这里添加了以下代码,以通过以下代码实例化我的rootViewClass(NativeLabelSwift):-
let sb = UIStoryboard(name: "mystoryboard", bundle: Bundle.main)
let rootViewController: UIViewController =
sb.instantiateViewController(withIdentifier:
"NativeLabelSwiftViewController") as UIViewController
NavigationLabelSwiftViewController.swift(自定义视图类):-
导航控制器有一个UIViewController类(NativeLabelSwiftViewController),该类在选项卡栏中有一个按钮。按下按钮后,我需要访问导航控制器方法。但我无法使用以下代码获取导航控制器:-
let navViewController = self.navigationController as?
NavigationController; //this is NIL
但是
let uiViewController = self.navigationController as?
UINavigationController; //this is not NIL
您没有在AppDelegate中引用自定义导航控制器。您使用了
UINavigationController(rootViewController:rootViewController)
如果要使用脚本导航控制器,请在AppDelegate中使用-
let nav = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NavigationController") as! NavigationController
window = UIWindow(frame: UIScreen.main.bounds)
window!.rootViewController = nav
window!.makeKeyAndVisible()
return true
注意:不要忘记为情节提要导航控制器添加标识符。您在AppDelegate中没有引用自定义导航控制器。您使用了
UINavigationController(rootViewController:rootViewController)
如果要使用脚本导航控制器,请在AppDelegate中使用-
let nav = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NavigationController") as! NavigationController
window = UIWindow(frame: UIScreen.main.bounds)
window!.rootViewController = nav
window!.makeKeyAndVisible()
return true
注意:别忘了为序列图像板导航控制器添加标识符。问题出在哪里?Anbu,您是否尝试过通过Cocoapods/Carthage使用库实现侧菜单?它不会直接回答你的问题,但它可能会解决您的问题。@dahiya_boy请在评论之前阅读代码。在onBtn2Clicked函数中,我得到的导航控制器参考值为零。@edopelawi感谢您的建议,但因为我想在没有任何库的情况下实现自己。@anbuselvan如果它解决了您的问题,您能接受我的答案吗?有什么问题吗,您是否尝试过通过Cocoapods/Carthage使用库实现侧菜单?它不会直接回答你的问题,但它可能会解决您的问题。@dahiya_boy请在评论之前阅读代码。在onBtn2Clicked函数中,我得到的导航控制器的引用为零。@edopelawi感谢您的建议,但因为我想实现自己,而不需要任何库。@anbuselvan如果它解决了您的问题,您能接受我的答案吗?不需要
if-else
此处。此处不需要if-else
。