我一直在尝试通过编程方式重做我的应用程序。(不使用故事板)我快完成了,除了手动制作导航控制器。目前,我只有一个视图控制器,当然还有appDelegate导航控制器将用于应用程序中的所有页面。如果有人能帮助我,或者发送一个链接到一些适当的文档,以编程的方式来做这件事,我将不胜感激。编辑:我忘了说这是斯威夫特的。
lfapxunr1#
雨燕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() }
字符串
雨燕4+:雨燕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() }
eh57zj3b2#
对于那些使用SceneDelegate.swift构建较新项目的用户,您可以使用'var窗口:UIWindow?“,而不是AppDelegate中已删除的”var窗口
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 }
nkhmeac63#
我建议使用以下框架启动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 }
svgewumm4#
下面是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() } }
qyyhg6bp5#
试试这个,它会指导你如何使用导航控制器。Programatically creating UINavigationController in iOSAppDelegate.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
应用委托.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]; }
x33g5p2x6#
这可能有点过头了,但我发现我经常这样做,所以我只是扩展了UIViewController并创建了一个embedInNavigationController方法,所以现在我可以调用viewController.embedInNavigationController。
extension UIViewController { func embedInNavigationController() -> UINavigationController { return UINavigationController(rootViewController: self) } }
7gcisfzg7#
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() }
7条答案
按热度按时间lfapxunr1#
雨燕1、2:
字符串
雨燕4+:雨燕5+
eh57zj3b2#
类型“AppDelegate”的值没有成员“window”
对于那些使用SceneDelegate.swift构建较新项目的用户,您可以使用'var窗口:UIWindow?“,而不是AppDelegate中已删除的”var窗口
nkhmeac63#
我建议使用以下框架启动AppDelegate:
1)尽可能使用let!
2)UINavigationController具有rootViewController属性。
svgewumm4#
下面是SceneDelegate类中的另一个镜头:
qyyhg6bp5#
试试这个,它会指导你如何使用导航控制器。
Programatically creating UINavigationController in iOS
AppDelegate.h
应用委托.m
然后,当你想推送另一个视图控制器时,简单地使用下面的代码移动到另一个视图控制器。
x33g5p2x6#
这可能有点过头了,但我发现我经常这样做,所以我只是扩展了UIViewController并创建了一个embedInNavigationController方法,所以现在我可以调用viewController.embedInNavigationController。
7gcisfzg7#