NSButton 状态问题
NSButton state issues
我有两个 ViewController。
在 VC1 上我有搜索条件,在 VC2 上我有搜索结果。如果您想从 VC2 转到 VC1,则 VC2 被关闭。
在 VC1 上,我有一个 NSButton(样式检查,类型切换),默认情况下我希望它处于打开状态。 NSButton 的目的是在结果中包含照片。
如果用户取消选中按钮并按搜索,它将继续到 VC2 显示没有照片的搜索结果。
但是,当用户返回 VC1 进行新的搜索时,就会出现不需要的行为:
NSButton 未选中(我希望默认情况下选中它,每次用户在 VC1 时。此外,该按钮为零。
为什么会这样,我怎样才能让它成为每次关闭 VC2 时都要勾选的按钮框?
我尝试启用它并将其设置为 ONState,但由于它为 nil,它会崩溃。
我已经设法通过添加
使其按照我想要的方式执行
switch.state=1
就在执行从 VC1 到 VC2 的转接之前。
但是,我认为这不是最优雅的解决方案,因为按钮仍然是 nil。
更新:
我发现问题发生在从 VC1 到 VC2 时,VC1 变为 nil,当 VC2 被关闭时,它也变为 nil。因此崩溃。一种解决方案是使用委托。
VC1:
class FirstViewController: UIViewController,SecondViewControllerProtocol {
@IBOutlet var firstName: UITextField!
@IBOutlet var lastName: UITextField!
@IBOutlet var subscribeSwitch: UISwitch!
@IBAction func goToSecondVC(_ sender: Any) {
let viewController = self.storyboard?.instantiateViewController(withIdentifier: String(describing: SecondViewController.self)) as! SecondViewController
viewController.delegate = self
self.present(viewController, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func dismissViewController() {
if let viewController = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController"){
subscribeSwitch.isOn=true
}
}
}
VC2:
protocol SecondViewControllerProtocol {
func dismissViewController()
}
class SecondViewController: UIViewController {
var delegate:SecondViewControllerProtocol!
@IBAction func goBackToFirstVC(_ sender: Any) {
self.dismiss(animated: true) {
self.delegate!.dismissViewController()
}
}
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.
}
}
要在每次打开控制器时设置状态,请使用方法
-(void)viewWillAppear
要让 viewController 相互通信,您可以实现委托。这是一个很好的教程:Link
另一种方法是与通知通信 -> Link
或者您可以在 prepareForSegue 等方法上设置值 - 取决于您用来实例化控制器的内容。
我有两个 ViewController。 在 VC1 上我有搜索条件,在 VC2 上我有搜索结果。如果您想从 VC2 转到 VC1,则 VC2 被关闭。
在 VC1 上,我有一个 NSButton(样式检查,类型切换),默认情况下我希望它处于打开状态。 NSButton 的目的是在结果中包含照片。
如果用户取消选中按钮并按搜索,它将继续到 VC2 显示没有照片的搜索结果。
但是,当用户返回 VC1 进行新的搜索时,就会出现不需要的行为: NSButton 未选中(我希望默认情况下选中它,每次用户在 VC1 时。此外,该按钮为零。
为什么会这样,我怎样才能让它成为每次关闭 VC2 时都要勾选的按钮框?
我尝试启用它并将其设置为 ONState,但由于它为 nil,它会崩溃。
我已经设法通过添加
使其按照我想要的方式执行switch.state=1
就在执行从 VC1 到 VC2 的转接之前。
但是,我认为这不是最优雅的解决方案,因为按钮仍然是 nil。
更新: 我发现问题发生在从 VC1 到 VC2 时,VC1 变为 nil,当 VC2 被关闭时,它也变为 nil。因此崩溃。一种解决方案是使用委托。
VC1:
class FirstViewController: UIViewController,SecondViewControllerProtocol {
@IBOutlet var firstName: UITextField!
@IBOutlet var lastName: UITextField!
@IBOutlet var subscribeSwitch: UISwitch!
@IBAction func goToSecondVC(_ sender: Any) {
let viewController = self.storyboard?.instantiateViewController(withIdentifier: String(describing: SecondViewController.self)) as! SecondViewController
viewController.delegate = self
self.present(viewController, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func dismissViewController() {
if let viewController = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController"){
subscribeSwitch.isOn=true
}
}
}
VC2:
protocol SecondViewControllerProtocol {
func dismissViewController()
}
class SecondViewController: UIViewController {
var delegate:SecondViewControllerProtocol!
@IBAction func goBackToFirstVC(_ sender: Any) {
self.dismiss(animated: true) {
self.delegate!.dismissViewController()
}
}
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.
}
}
要在每次打开控制器时设置状态,请使用方法
-(void)viewWillAppear
要让 viewController 相互通信,您可以实现委托。这是一个很好的教程:Link
另一种方法是与通知通信 -> Link
或者您可以在 prepareForSegue 等方法上设置值 - 取决于您用来实例化控制器的内容。