以编程方式创build一个navigationController(Swift)
我一直试图以编程方式重做我的应用程序的工作。 (不使用故事板)
我几乎完成,除了手动导航控制器。
我一直在做一些研究,但我找不到任何手动实现这个文件。 (我开始制作应用程序作为单一视图应用程序)
目前,我只有1个viewcontroller。 当然还有appDelegate
导航控制器将在应用程序的所有页面中使用。
如果任何人都可以帮助我,或者发送一个链接到一些适当的文件来做这个以编程方式,将不胜感激。
编辑:
我忘了提及它在斯威夫特。
试试这个更新
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 3 :
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()
它为我工作得很好…尝试一下..你会得到成功…
我会build议开始你的AppDelegate这个骨架:
1)尽可能使用!
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 }
试试这个。 它会指导你如何使用导航控制器。
在iOS中以编程方式创buildUINavigationController
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]; }
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() }