2010-04-23 36 views
3

我想創建一個uitableviewcontroller作爲一個模式的視圖控制器來編輯一些設置。我正在代碼中創建tableviewcontroller,我目前正在努力的事情是如何正確添加一個導航欄到控制器上,該控制器上將有一個「完成」按鈕:以編程方式將NavigationBar添加到UITableViewController?

a)不出現在tableview和 B)不滾動tableview?

發生這種情況時,我將navbar添加到控制器: [self.view addSubview:navigationBar]; 這增加了一個導航欄到控制器上,並且遮擋了表格的第一行,並且還滾動了視圖?

我也想過簡單地使用uiviewcontroller與單獨的tableview,但我喜歡在編輯tableviewcontroller給你的文本框時自動滾動tableview的功能。只是不知道如何設置此導航欄?

THX

回答

8

只需創建的UINavigationController作爲模態的ViewController,並添加tableview中作爲其根視圖控制器。

+0

THX,也許我沒有正確理解,但這似乎只是使用uiviewcontroller而不是uitableviewcontroller。我想使用一個uitableviewcontroller,所以當用戶試圖編輯鍵盤可能會遮蓋的某個文本字段時,我不必擔心滾動/調整視圖的大小。 – Tony 2010-04-23 18:42:30

+0

@Tony:當你創建模式視圖控制器時,也創建一個'UINavigationController'並添加視圖控制器。然後,呈現導航控制器。 – shosti 2010-04-23 21:54:47

6

將導航控制器用作modalviewController(如其他答案中的建議)。下面是代碼:

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

斯威夫特

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!!!") 
    } 
相關問題