Представление контроллера представления программно в быстром
Привет. Я пытаюсь преобразовать следующий объективный C-код в swift для перехода от одного контроллера представления к другому контроллеру представления при нажатии кнопки. любая помощь будет высоко оценена
Это взято из руководства Apple по программированию
- (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)
}
Ответы
Ответ 1
Вы хотите представить navController модально?
если да, это ответ
self.presentViewController(navController, animated: true, completion: nil)
"self" - это текущий контроллер представления, который будет представлять 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)
}
}
Но,
Если вы хотите перемещаться между viewController в navigationController, вы можете использовать
self.navigationController.pushViewController(viewControllerToPush, animated: true)
Ответ 2
Я сделал простое решение. Вот оно..
func actioncall () {
let loginPageView = self.storyboard?.instantiateViewControllerWithIdentifier("LoginPageID") as! ViewController
self.presentViewController(loginPageView, animated: true, completion: nil)
}