以编程方式创建NavigationController(Swift)

2020/12/01 18:31 · ios ·  · 0评论

我一直在尝试以编程方式重做我的应用程序上的工作。(不使用情节提要)

除了手动制作导航控制器外,我几乎完成了。

我一直在做一些研究,但找不到任何手动实现此方法的文档。(我开始将应用程序制作为单视图应用程序)

目前,我只有1个ViewController。当然是appDelegate

导航控制器将在应用程序的所有页面中使用。

如果有人可以帮助我,或发送指向一些适当文档的链接以编程方式进行此操作,将不胜感激。

编辑:

我忘了提到它在Swift中。

斯威夫特1,2:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
   self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
   var nav1 = UINavigationController()
   var mainView = ViewController(nibName: nil, bundle: nil) //ViewController = Name of your controller
   nav1.viewControllers = [mainView]
   self.window!.rootViewController = nav1
   self.window?.makeKeyAndVisible()
}

Swift 4+:和Swift 5+

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
   self.window = UIWindow(frame: UIScreen.main.bounds)
   let nav1 = UINavigationController()
   let mainView = ViewController(nibName: nil, bundle: nil) //ViewController = Name of your controller
   nav1.viewControllers = [mainView]
   self.window!.rootViewController = nav1
   self.window?.makeKeyAndVisible()
}

我建议使用此框架启动您的AppDelegate:

1)尽可能使用let

2)UINavigationController具有rootViewController属性。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    let viewController = ViewController(nibName: nil, bundle: nil) //ViewController = Name of your controller
    let navigationController = UINavigationController(rootViewController: viewController)

    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    self.window?.rootViewController = navigationController
    self.window?.makeKeyAndVisible()

    return true
}

类型“ AppDelegate”的值没有成员“窗口”

对于那些使用SceneDelegate.swift构建较新项目的人,可以使用“ var window:UIWindow?” 在SceneDelegate中代替在AppDelegate中删除的“可变窗口”

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = (scene as? UIWindowScene) else { return }

    window?.windowScene = windowScene
    window?.makeKeyAndVisible()

    let viewController = ViewController()
    let navViewController = UINavigationController(rootViewController: viewController)
    window?.rootViewController = navViewController
}

试试这个。它将指导您如何使用导航控制器。

在iOS中以编程方式创建UINavigationController

AppDelegate.h

    #import <UIKit/UIKit.h>
    #import "LoginViewController.h"

    @interface AppDelegate : UIResponder <UIApplicationDelegate>

    @property (strong, nonatomic) UIWindow *window;
    @property (strong,nonatomic) UINavigationController *navigationController;
    @property (strong,nonatomic) LoginViewController *loginVC;

    @end

AppDelegate.m

    #import "AppDelegate.h"
    #import "LoginViewController.h"

    @implementation AppDelegate

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

   self.loginVC = [[LoginViewController alloc]initWithNibName:nil bundle:nil];
   self.loginVC.title = @"Login Page";

   self.navigationController = [[UINavigationController alloc]initWithRootViewController:self.loginVC];

   self.window.rootViewController = self.navigationController;
   [self.window makeKeyAndVisible];
  }

然后,当您要推送另一个视图控制器时,只需使用以下代码即可移至另一个视图控制器。

- (IBAction)pushMyProfileView:(id)sender
{
    self.myProfileVC = [[MyProfileViewController alloc]initWithNibName:nil bundle:nil];
    [appDelegate.navigationController pushViewController:self.myProfileVC animated:YES];
}

这是SceneDelegate类的另一种用法:

var window: UIWindow?

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        if let windowScene = scene as? UIWindowScene {

                let window = UIWindow(windowScene: windowScene)    
                let navController = UINavigationController()
                let viewController = ViewController()

                navController.viewControllers = [viewController]            
                window.rootViewController = navController
                self.window = window
                window.makeKeyAndVisible()
        }
}
 self.window = UIWindow(frame: UIScreen.main.bounds) 
 let storyboard = UIStoryboard(name: "Main", bundle: nil) 
 let storyboard_Secondary = UIStoryboard(name: "Secondary", bundle: nil) 
 var initialViewController = UIViewController() 

 let aUser = CommonMethods.loadCustomObject("\(Constants.kUserProfile)") as? User  
 if aUser?.respCode == 1 { 
    initialViewController = storyboard_Secondary.instantiateViewController(withIdentifier: "MainTabVC")
    UIApplication.shared.statusBarStyle = .lightContent
    let navigationController = UINavigationController(rootViewController: initialViewController)
    navigationController.isNavigationBarHidden = true
    self.window!.rootViewController = navigationController
    self.window!.makeKeyAndVisible() 
}
本文地址:http://ios.askforanswer.com/yibianchengfangshichuangjiannavigationcontrollerswift.html
文章标签: ,   ,  
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!

文件下载

老薛主机终身7折优惠码boke112

上一篇:
下一篇:

评论已关闭!