Adding NavigationBar software to UITableViewController?

I am trying to create a uitableviewcontroller as a modal viewcontroller to edit some parameters. I am creating a tableviewcontroller in code, and currently I am struggling with how to correctly add the navigation bar to the controller, which will have the "Finish" button, which:

a) does not appear on top of the table; and b) does not scroll with the table view ??

This happens when I add navbar to the contoller with: [self.view addSubview: navigationBar]; Does this add a navigator to the controller, which comes out from above and shades the tables with the first row, as well as scrolls with a view?

I also thought about just using a uiviewcontroller with a separate table view, but I like the automatic scrolling functionality of tableview when editing a text field that the tableviewcontroller editor gives you. I just can’t understand how to configure this navigator?

THX

+3
source share
3 answers

Just create a UINavigationcontroller as a modal viewcontroller and add a tableview as your root view controller.

+8
source

Use the navigation controller as modalviewController (as suggested in another answer). Here is the code:

UINavigationController *Controller = [[UINavigationController alloc] init];
            //LoginViewController is a sub class of UITableviewController
        LoginViewController *controller = [[LoginViewController alloc] init];
        Controller.viewControllers=[NSArray arrayWithObject:controller];

        [self presentModalViewController:Controller animated:YES];
        [controller release];
        [Controller release];
+6
source

Swift:

AppDelegate.swift

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
       /*
          #1. Instantiate a navigation controller and add the table view controller 
          as a child view controller.
       */
       let navigationController = UINavigationController()
       // Instantiate your desired ViewController
       let storyboard = UIStoryboard(name: UIStoryboardName.Main.rawValue, bundle: nil)
       let tableViewController = storyboard.instantiateViewControllerWithIdentifier("TableViewControllerID")
                    navigationController.addChildViewController(tableViewController)

       /*
          #2. Then we set the title of the navigation bar and add two bar button items.
       */
       // We set the title of the navigation bar.
       tableViewController.navigationItem.title = "My Title"

       // Create left and right button for navigation item.
       let leftButton =  UIBarButtonItem(title: "Save", style:  UIBarButtonItemStyle.Plain, target: tableViewController, action: "saveButtonClicked:")
       let rightButton = UIBarButtonItem(title: "Right", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)

       // Create two buttons for the navigation item.
       tableViewController.navigationItem.leftBarButtonItem = leftButton
       tableViewController.navigationItem.rightBarButtonItem = rightButton

       /*
           #3. To finish we set the root view controller with the navigation controller.
       */            
       self.window?.rootViewController = navigationController
       self.window?.makeKeyAndVisible()

       return true
    }

TableViewController.swift

    // Method called when the user clicks on the Save bar button item.
    func saveButtonClicked(sender: UIBarButtonItem) {
        // Do something.
        print("Save bar button item has been clicked!!!")
    }
0
source

Source: https://habr.com/ru/post/1742513/


All Articles