gpt4 book ai didi

swift - 在 swift 中以编程方式呈现 View Controller

转载 作者:IT王子 更新时间:2023-10-29 05:25:07 26 4
gpt4 key购买 nike

您好,我正在尝试将以下 objective-c 代码转换为 swift,以便在单击按钮时从一个 View Controller 导航到另一个 View Controller 。任何帮助将不胜感激

这是摘自苹果的编程指南

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

我的代码如下所示,但不确定如何在导航 Controller 中实现,在 Storyboard 中我的 mainSectionViewController 嵌入了导航 Controller

    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)


}

最佳答案

你想以模态方式呈现 navController 吗?

如果是,这就是答案

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

“self”是将呈现 navController 的当前 View Controller

然后这样说,

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)
}

}

但是,

如果想在navigationController中的viewController之间导航,可以使用

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

关于swift - 在 swift 中以编程方式呈现 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27326183/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com