我正在做一个小项目,在我的 Storyboard上有几个独立的 View Controller ,并创建了一个显示 UITableViewController
的按钮,但我需要一种方法让用户返回主页。我在 MainTableVC
顶部创建了一个按钮,当我运行该应用程序时,它会显示该按钮。我需要的是让按钮简单地显示我的主视图 Controller ,它被称为 View Controller 。我仍在学习,所以向前迈出的每一步对我来说都是一次飞跃。这是 viewDidLoad
的代码,我在其中创建了按钮。
let button1 = UIButton(frame: CGRect(origin: CGPoint(x: self.view.frame.width / 2 - 25, y: self.view.frame.height - 70), size: CGSize(width: 50, height: 50)))
button1.backgroundColor = UIColor.blue
self.navigationController?.view.addSubview(button1)
我也确实在类中列出了一个 UITableViewController
。谢谢大家的帮助,我很感激。所以总而言之,我需要将我创建的 button1
连接到 viewcontroller,(因为你不能只将一个按钮放在 tableview 的顶部,然后只需控制点击另一个 View 并设置“显示”。这实际上更容易说,我需要编写简单的 cntrl 从一个按钮拖动到 View 并设置“显示”的方面。非常感谢大家。
最佳答案
据我所知..您可以在 MainViewController 中使用手动创建一个按钮并向其添加 Target
let button1 = UIButton(frame: CGRect(origin: CGPoint(x: self.view.frame.width / 2 - 25, y: self.view.frame.height - 70), size: CGSize(width: 50, height: 50)))
button1.backgroundColor = UIColor.blue
button1.addTarget(self, action: #selector(MainViewController.actionOpenTableView(_:)), forControlEvents: .TouchUpInside)
self.navigationController?.view.addSubview(button1)
同时在你的类中创建函数 actionOpenTableView 捕获 按钮 1 的目标,您可以在其中使用 segue 或以编程方式推送/呈现 TableViewController(:此处为 MyTableViewController)。
func actionOpenTableView(btn:UIButton){
//if using segue of motherboard
// self.performSegueWithIdentifier("MoveToTableView", sender: self)
//if using programmically
let tableVC = self.storyboard?.instantiateViewControllerWithIdentifier("MyTableVC")as! MyTableViewController
self.navigationController?.pushViewController(tableVC, animated: true)
}
如果以编程方式使用,请确保将 StoryboardID 提供给 Storyboard 中的 TableviewController。
一旦你进入你的 TableViewController 你可以使用默认的后退按钮..或者创建自定义方法如下从 MyTableViewController 返回你的 MainViewController
@IBAction func mBackActionBtn(sender: AnyObject) {
self.navigationController?.popViewControllerAnimated(true)
}
关于ios - 如何连接 UITableVC 中制作的按钮以在 Story Board 中显示单独的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41672929/