Iphone 是否以编程方式将导航栏添加到UITableViewController?

Iphone 是否以编程方式将导航栏添加到UITableViewController?,iphone,objective-c,Iphone,Objective C,我正在尝试创建uitableviewcontroller作为模态viewcontroller来编辑某些设置。我正在用代码创建tableviewcontroller,目前我正在努力解决的问题是如何正确地向控制器添加导航栏,该导航栏上会有一个“完成”按钮: a) 不显示在表视图的顶部,并且 b) 不随tableview一起滚动 将导航栏添加到控制器时,会发生这种情况: [self.view addSubview:navigationBar]; 这将向控制器添加一个导航栏,该导航栏位于顶部,遮住表格的

我正在尝试创建uitableviewcontroller作为模态viewcontroller来编辑某些设置。我正在用代码创建tableviewcontroller,目前我正在努力解决的问题是如何正确地向控制器添加导航栏,该导航栏上会有一个“完成”按钮:

a) 不显示在表视图的顶部,并且 b) 不随tableview一起滚动

将导航栏添加到控制器时,会发生这种情况: [self.view addSubview:navigationBar]; 这将向控制器添加一个导航栏,该导航栏位于顶部,遮住表格的第一行,并与视图一起滚动

我还考虑过简单地将uiviewcontroller与单独的tableview一起使用,但是我喜欢在编辑tableviewcontroller提供的文本字段时自动滚动tableview的功能。只是不知道如何设置这个导航栏


thx

只需创建UINavigationcontroller作为模态视图控制器,并添加tableview作为其根视图控制器。

使用导航控制器作为模态视图控制器(如另一个答案中所建议)。代码如下:

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];

在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
    }
    // 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!!!")
    }
TableViewController.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
    }
    // 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!!!")
    }

Thx,也许我没有正确理解,但这似乎类似于只使用uiviewcontroller而不是uitableviewcontroller。我想使用uitableviewcontroller,这样当用户试图编辑其中一个键盘可能会模糊的文本字段时,我就不必担心滚动/调整视图大小了。@Tony:创建模式视图控制器时,还要创建一个
UINavigationController
并将视图控制器添加到其中。然后,介绍导航控制器。