4

I am trying to create a uitableviewcontroller as a modal viewcontroller to edit some settings. I am creating the tableviewcontroller in code and the thing i am struggling with currently is how to correctly add a navigation bar to the controller which will have a "Done" button on it that:

a) doesnt appear on top of the tableview and b) does not scroll with the tableview??

This happens when i add the navbar to the contoller with: [self.view addSubview:navigationBar]; This adds a navbar to the controller which goes on top and obscures the tables first row and also scrolls with the view?

I also thought about simply using a uiviewcontroller with a separate tableview however i like the funcitonality of automatically scrolling the tableview when editing a textfield that the tableviewcontroller gives you. Just cant figure how to setup this navbar??

thx

3 Answers 3

9

Just create UINavigationcontroller as the modal viewcontroller, and add the tableview as its root view controller.

Sign up to request clarification or add additional context in comments.

2 Comments

Thx, maybe i not understanding correctly but this seems similar to just using a uiviewcontroller instead of a uitableviewcontroller. I would like to use a uitableviewcontroller so i dont have to worry about scrolling/resizing the view when the user tries to edit one of the textfields which the keyboard may obscure.
@Tony: when you create the modal view controller, also create a UINavigationController and add the view controller to it. Then, present the navigation controller.
7

Use Navigation controller as modalviewController(as suggested in the other 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];

Comments

-1

In 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!!!")
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.