AppDelegate.swift
SWIFT 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. self.window = UIWindow(frame: UIScreen.main.bounds) // create navigation let nav1 = UINavigationController() // instantiate your basic view controller let mainView = ViewController(nibName: nil, bundle: nil) //ViewController = Name of your controller // set our navigation's view array nav1.viewControllers = [mainView] // app root view controller is on navigation self.window!.rootViewController = nav1 self.window?.makeKeyAndVisible() self.window?.backgroundColor = UIColor.cyan return true } func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. } func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. } func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } } |
ViewController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import UIKit import CoreLocation class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.red let openAction = #selector(openNewWindow(sender:)) let openWindowBtn = UIButton(type: UIButtonType.custom) openWindowBtn.addTarget(self, action: openAction, for: UIControlEvents.touchUpInside) openWindowBtn.frame = CGRect(x: 80, y: 80, width: 200, height: 80) openWindowBtn.setTitle("Push Window", for: .normal) openWindowBtn.backgroundColor = UIColor.orange self.view.addSubview(openWindowBtn) } func openNewWindow(sender: Any) { print("CLICKED") self.navigationController?.pushViewController(CustomViewController(), animated: true) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } deinit { print("\(type(of: self), #function) - deinit") } } |
CustomViewController.swift
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import UIKit class CustomViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.brown // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } deinit { print("\(type(of: self), #function)") } } |
OBJECTIVE C
The whole idea is that your window strongs UINavigationController, and UINavigationController strongs your main ViewController.
The UINavigationController is set up in your AppDelegate. Then your UINavigationController is init-ed with your main view controller.
Then, in your main view controller, use self.navigationItem to set up the buttons and title.
UINavigationController
First we create a UINavigationController property.
1 2 3 |
@interface AppDelegate () @property(nonatomic, retain) UINavigationController * navigationController; @end |
Then, we create the UINavigationController and init it with the main view controller of your project.
Set the UINavigationController as the root view controller of our window.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { ViewController * mainViewController = [[ViewController alloc] init]; self.navigationController = [[UINavigationController alloc] initWithRootViewController: mainViewController]; self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [self.window setRootViewController:self.navigationController]; self.window.backgroundColor = [UIColor blackColor]; [self.window makeKeyAndVisible]; return YES; } |
In your main view controller, make sure in your viewDidLoad, you set the navigation button like so:
1 2 3 4 5 6 7 8 9 10 |
- (void)viewDidLoad { [super viewDidLoad]; // UI set up UIBarButtonItem * navButton = [[UIBarButtonItem alloc] initWithTitle:@"add" style:UIBarButtonItemStylePlain target:self action:@selector(addPerson:)]; self.navigationItem.rightBarButtonItem = navButton; } |
The nav button has a button responder:
1 2 3 |
-(void)addPerson:(id)sender { } |
This means when you click on the nav button, we want to navigate to a new view controller. Hence, let’s implement the view controller that we want to navigate to.
File >> New >> File >> Cocoa Touch Class
Put in AddViewController for the “class name”.
Put in UIViewController for the “subclass of”.
Then import AddViewController to your main view controller, and implement the button responder like so:
1 2 3 4 5 |
-(void)addPerson:(id)sender { AddViewController * vc = [[AddViewController alloc] init]; [self.navigationController pushViewController:vc animated:YES]; } |
Make sure you set background color for every view controller because if its default as clear, there may be animation issues.