2014-12-05 107 views
10

您好我想將下列目標C代碼轉換爲swift,以便在單擊按鈕時從一個視圖控制器導航到另一個視圖控制器。任何幫助,將不勝感激在swift中以編程方式呈現視圖控制器

這是從蘋果的編程指南採取

- (void)add:(id)sender { 
// Create the root view controller for the navigation controller 
// The new view controller configures a Cancel and Done button for the 
// navigation bar. 
    RecipeAddViewController *addController = [[RecipeAddViewController alloc] 
        init]; 

// Configure the RecipeAddViewController. In this case, it reports any 
// changes to a custom delegate object. 
    addController.delegate = self; 

// Create the navigation controller and present it. 
    UINavigationController *navigationController = [[UINavigationController alloc] 
         initWithRootViewController:addController]; 
    [self presentViewController:navigationController animated:YES completion: nil]; 
    } 

我的代碼如下所示,但不知道如何在導航控制器實現,在故事板我mainSectionViewController嵌有導航控制器

func sectionBtnClicked (sender: UIButton!) { 


     let sectionController = self.storyboard?.instantiateViewControllerWithIdentifier("mainSectionsVC") as mainSectionViewController 


     let navController = UINavigationcontroler. ///not sure what actually comes here, any help would be appreciated 


     self.presentViewController(navController, animated: true, completion: nil) 


} 

回答

17

是否要模態地顯示navController?

如果是的,這就是答案

self.presentViewController(navController, animated: true, completion: nil) 

「自我」 是將目前navController

並把它像這樣當前視圖控制器,

class ViewController: UIViewController {  

    override func viewDidLoad() { 
     super.viewDidLoad() 

     var theButton = UIButton() 

     // Add the event to button 
     theButton.addTarget(self, action: "buttonTouchInside:", forControlEvents: .TouchUpInside) 

     self.view.addSubview(theButton) 
    } 

    func buttonTouchInside(sender:UIButton!) 
    { 
     // When the button is touched, we're going to present the view controller 

     // 1. Wrap your view controller within the navigation controller 

     let navController = UINavigationController(rootViewController: yourViewController) 

     // 2. Present the navigation controller 

     self.presentViewController(navController, animated: true, completion: nil) 
    } 

} 

但是,

如果你想瀏覽navigationControlle中的viewController r,你可以用

self.navigationController.pushViewController(viewControllerToPush, animated: true) 
14

我做了一個簡單的解決方案。這裏是...

func actioncall() { 
    let loginPageView = self.storyboard?.instantiateViewControllerWithIdentifier("LoginPageID") as! ViewController 
    self.presentViewController(loginPageView, animated: true, completion: nil) 
} 
相關問題