【发布时间】:2018-01-19 23:25:36
【问题描述】:
所以我的 iOS 应用有以下布局。
我打算在purpleVC 中放置一个表格视图来控制Green viewcontroller...顶部peachVC 中将包含需要更改的文本。我只是不确定如何从另一个视图控制器控制一个视图控制器。这包括在单击 GreenVC 上的按钮时使紫色滑入和滑出。我知道有一些课程可以做到这一点,但我也想学习。
测试代表:
主视图控制器
import UIKit
protocol Purpleprotocol {
func buttonpressed()
}
protocol Greenprotocol {
}
extension UIViewController {
func alert(message: String, title: String = "") {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(OKAction)
self.present(alertController, animated: true, completion: nil)
}
}
class MainViewController: UIViewController,Purpleprotocol,Greenprotocol {
weak var infoNav : UINavigationController?
weak var greenVC: GreenVC?
weak var purpleVC: PurpleVC?
weak var peachVC: PeachVC?
func buttonpressed() {
alert(message: "This is message")
print("buttonpressed")
let date = Date()
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
greenVC?.greenlabel.text = String(hour) + ":" + String(minutes)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "contentSegue" {
let infoNav = segue.destination as! UINavigationController
}
}
}
PURPLEVIEW 控制器
class PurpleVC: UIViewController {
var delegate: Purpleprotocol?
@IBAction func butclick(_ sender: UIButton) {
alert(message: "infunction")
delegate?.buttonpressed()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
谢谢 回复
【问题讨论】:
-
不完全确定这是否可以通过导航控制器工作,我做了类似的事情,但从一个视图控制器直接到另一个视图控制器。本质上,想法是创建一个委托,并符合第二个 VC 中的委托。第一个 VC 在第二个 View 控制器中触发一个方法(以所需的方式控制它),然后当我想执行 segue 时,我会加载更改后的第二个 VC 并显示它。也许类似的东西可以帮助你。
标签: ios viewcontroller uicontainerview