2017-01-16 83 views
1
一个单独的视图

我与我的故事板的几个独立的视图控制器一个小项目工作,并具备创建了一个按钮,显示一个UITableViewController,但我需要一种让用户回到主页的方式。我在MainTableVC上创建了一个按钮,当我运行该应用程序时,它会显示按钮。我需要的是按钮只显示我的主视图控制器,它被称为视图控制器。我仍然在学习,所以向前迈进对我来说是一个飞跃。这里是viewDidLoad的代码,我创建了这个按钮。如何连接在UITableVC做了按钮显示在故事板

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在类上市。谢谢大家的帮助,我很感激。因此,要总结,我需要连接我创建到ViewController的button1,(因为你不能只是把一个实现代码如下顶部的按钮,然后就控制单击以另一种观点认为,把“秀”。这实际上使得它更容易我需要编写简单的cntrl从一个按钮拖动到一个视图,并设置“显示”的方面。感谢所有人。

+0

如何显示您的UITableView?你是用'self.navigationController.pushViewController'将它推到导航堆栈上,还是用'self.presentViewController'模态地呈现它? – JoGoFo

+0

我确实将它设置为导航控制器JoGoFo,如果这就是你要求的。 – Nivix

+0

JoGoFo它是self.presentViewController? – Nivix

回答

0

多达我明白..你可以使用手动创建您的MainViewController一个按钮并添加标记把它

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) 

另外在你的类捕获按钮1的 目标,您可以创建功能actionOpenTableView使用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) 
} 

如果使用pprogrammically确保您向Storyboard中的TableviewController提供StoryboardID。

enter image description here 一旦你得到你的TableViewController您可以使用默认的后退按钮..或创建作为遵循从MyTableViewController

又回到了你的MainViewController自定义的方法
@IBAction func mBackActionBtn(sender: AnyObject) { 
     self.navigationController?.popViewControllerAnimated(true) 
    } 
0

首先,你不应该直接添加一个按钮到你的导航控制器。要添加自定义按钮,而不是使用以下命令:

self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Title", style: .plain, target: self, action: #selector(navButtonPressed)) 

然后,您可以执行该操作的方法:

func navButtonPressed() { 
    // if pushed onto nav vc: 
    // _ = self.navigationController?.popViewController(animated: true) 

    // if presented modally (as suggested in your comment: 
    self.dismiss(animated: true, completion: nil) 
} 
+0

以及我应该为#selector(navButtonPushed)放置什么?谢谢JoGoFo – Nivix

+0

'#selector'是一个关键字,'navButtonPressed'是你希望在用户按下按钮时调用的方法的名称。这是我答案的第二部分。 – JoGoFo

+0

JoGoFo我的代码不在这里编译是一个截图请检查出来。 http://imgur.com/a/hQamM – Nivix