我有一个带有分段控件的视图控制器。我想使用分段控件更改视图控制器。我感觉自己已经接近了,但是在测试实现时视图没有加载。我在viewDidLoad中添加了触觉反馈作为测试,当我使用分段控件时感觉到了反馈。
这是我的View Controller代码:
import UIKit
class ViewController: UIViewController {
var content: UIViewController?
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(changeView1), name: NSNotification.Name("notifyButtonPressed0"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(changeView2), name: NSNotification.Name("notifyButtonPressed1"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(changeView3), name: NSNotification.Name("notifyButtonPressed2"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(changeView4), name: NSNotification.Name("notifyButtonPressed3"), object: nil)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setContent(_content: UIViewController?) {
if (self.content != nil)
{
self.content?.view.removeFromSuperview()
content?.removeFromParentViewController()
}
let _content = content
self.addChildViewController(_content!)
content?.didMove(toParentViewController: self)
self.view.addSubview((content?.view)!)
}
@objc func changeView1(_ kMIDIMessageSendErr: Any?) {
let storyboardName = "Main"
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
let vc: UIViewController = storyboard.instantiateViewController(withIdentifier: "View1")
self.content = vc
self.content?.view.frame = CGRect(x: 10, y: 65, width: 300, height: 450)
}
@objc func changeView2(_ kMIDIMessageSendErr: Any?) {
let storyboardName = "Main"
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
let vc: UIViewController = storyboard.instantiateViewController(withIdentifier: "View2")
self.content = vc
self.content?.view.frame = CGRect(x: 10, y: 65, width: 300, height: 450)
}
@objc func changeView3(_ kMIDIMessageSendErr: Any?) {
let storyboardName = "Main"
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
let vc: UIViewController = storyboard.instantiateViewController(withIdentifier: "View3")
self.content = vc
self.content?.view.frame = CGRect(x: 10, y: 65, width: 300, height: 450)
}
@objc func changeView4(_ kMIDIMessageSendErr: Any?) {
let storyboardName = "Main"
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
let vc: UIViewController = storyboard.instantiateViewController(withIdentifier: "View4")
self.content = vc
self.content?.view.frame = CGRect(x: 10, y: 65, width: 300, height: 450)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
}
这是我通过Interface Builder添加到视图控制器中的NSObject类。
import UIKit
class ButtonHandler: NSObject {
var segmentControl: UISegmentedControl?
@IBAction func handleButton1(_ sender: Any) {
NotificationCenter.default.post(name: NSNotification.Name("notifyButtonPressed"), object: self)
}
@IBAction func segmentChanged(_ sender: UISegmentedControl) {
segmentControl = sender
switch segmentControl?.selectedSegmentIndex {
case 0:
NotificationCenter.default.post(name: NSNotification.Name("notifyButtonPressed0"), object: self)
case 1:
NotificationCenter.default.post(name: NSNotification.Name("notifyButtonPressed1"), object: self)
case 2:
NotificationCenter.default.post(name: NSNotification.Name("notifyButtonPressed2"), object: self)
case 3:
NotificationCenter.default.post(name: NSNotification.Name("notifyButtonPressed3"), object: self)
default:
break
}
}
}
我认为问题出在我的ViewController代码中,但我不知道为什么它没有显示我的视图控制器。 ViewDidLoad开始运行,但是我什么也没看到。
编辑:我将在Objective-C中工作的函数转换为Swift。也许您可以再次检查我是否在这里出错。
-(void)setContent:(UIViewController *)content
if(_content)
{
[_content.view removeFromSuperview];
[_content removeFromParentViewController];
}
_content = content;
[self addChildViewController:_content];
[content didMoveToParentViewController:self];
[self.view addSubview:_content.view];
}