Swift 3 - 无法执行功能
Swift 3 - Unable to execute a function
以下是我的代码。我还发布了截图。
Button_CustomSegmentValueChanged 是我制作的自定义分段控件。它是一个单独的按钮,工作正常,但我也想从 "SwipedRight" 函数内部使用 运行 "Button_CustomSegmentValueChanged" 函数,但我无法传递正确的开关值来这样做。需要帮助。
有人吗?
import UIKit
class CredentialsViewController: UIViewController {
@IBOutlet weak var loginView: UIView!
@IBOutlet weak var signupView: UIView!
var currentSelectedView = 0
override func viewDidLoad() {
super.viewDidLoad()
let swipeR = UISwipeGestureRecognizer(target: self, action: #selector(CredentialsViewController.SwipedRight(swipe:)))
swipeR.direction = .right
view.addGestureRecognizer(swipeR)
let swipeL = UISwipeGestureRecognizer(target: self, action: #selector(CredentialsViewController.SwipedLeft(swipe:)))
swipeL.direction = .left
view.addGestureRecognizer(swipeL)
//Hide view
signupView.transform = CGAffineTransform(translationX: signupView.frame.size.width, y: 0)
}
@IBAction func Button_CustomSegmentValueChanged(_ sender: CustomSegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
LoadLoginView()
break
case 1:
LoadSignupView()
break
default:
break
}
}
func SwipedRight(swipe : UISwipeGestureRecognizer){
if currentSelectedView == 1 {
Button_CustomSegmentValueChanged(0)
//LoadLoginView()
}
}
func SwipedLeft(swipe : UISwipeGestureRecognizer){
if currentSelectedView == 0 {
//LoadSignupView()
}
}
func LoadLoginView(){
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
self.signupView.transform = CGAffineTransform(translationX : self.loginView.frame.size.width, y : 0)
self.loginView.transform = .identity
self.currentSelectedView = 0
})
}
func LoadSignupView(){
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
self.signupView.transform = .identity
self.loginView.transform = CGAffineTransform(translationX: self.loginView.frame.size.width, y: 0)
self.currentSelectedView = 1
})
}
}
Screenshot of my code and error
您传递的是 Int
,但所需的实际类型是 CustomSegmentedControl
。要简单地解决这个问题,只需为您的 CustomSegmentedControl
创建 IBOutlet
并将其作为参数传递给 Button_CustomSegmentValueChanged
方法。
func SwipedRight(swipe : UISwipeGestureRecognizer){
if currentSelectedView == 1 {
customSegmentOutlet.selectedSegmentIndex = 0
Button_CustomSegmentValueChanged(customSegmentOutlet)
//LoadLoginView()
}
}
我建议使用 CredentialSegmentedController 上的按钮数组来模仿更改段的行为
- 从您的自定义细分控件创建一个出口
然后使用下面的段控件修改您的代码
func SwipedRight(swipe : UISwipeGestureRecognizer) {
if currentSelectedView == 1 {
//Button_CustomSegmentValueChanged(0)
//LoadLoginView()
let btn = customSegmentOutlet.buttons[0]
customSegmentOutlet.buttonTapped(btn)
}
}
func SwipedLeft(swipe : UISwipeGestureRecognizer){
if currentSelectedView == 0 {
//LoadSignupView()
let btn = customSegmentOutlet.buttons[1]
customSegmentOutlet.buttonTapped(btn)
}
}
假设你的CustomSegmentedControl
是UISegmentedControl
的子类,我修改了几行代码
@IBAction func button_CustomSegmentValueChanged(_ sender: UISegmentedControl?) {
// guard sender for nil before use
}
并且调用这个时
func swipedRight(swipe : UISwipeGestureRecognizer){
if currentSelectedView == 1 {
let seg = UISegmentedControl()
seg.selectedSegmentIndex = 0
Button_CustomSegmentValueChanged(seg)
}
}
如果 CustomSegmentedControl
不是 UISegmentedControl
的子类,请更改它。
以下是我的代码。我还发布了截图。
Button_CustomSegmentValueChanged 是我制作的自定义分段控件。它是一个单独的按钮,工作正常,但我也想从 "SwipedRight" 函数内部使用 运行 "Button_CustomSegmentValueChanged" 函数,但我无法传递正确的开关值来这样做。需要帮助。
有人吗?
import UIKit
class CredentialsViewController: UIViewController {
@IBOutlet weak var loginView: UIView!
@IBOutlet weak var signupView: UIView!
var currentSelectedView = 0
override func viewDidLoad() {
super.viewDidLoad()
let swipeR = UISwipeGestureRecognizer(target: self, action: #selector(CredentialsViewController.SwipedRight(swipe:)))
swipeR.direction = .right
view.addGestureRecognizer(swipeR)
let swipeL = UISwipeGestureRecognizer(target: self, action: #selector(CredentialsViewController.SwipedLeft(swipe:)))
swipeL.direction = .left
view.addGestureRecognizer(swipeL)
//Hide view
signupView.transform = CGAffineTransform(translationX: signupView.frame.size.width, y: 0)
}
@IBAction func Button_CustomSegmentValueChanged(_ sender: CustomSegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
LoadLoginView()
break
case 1:
LoadSignupView()
break
default:
break
}
}
func SwipedRight(swipe : UISwipeGestureRecognizer){
if currentSelectedView == 1 {
Button_CustomSegmentValueChanged(0)
//LoadLoginView()
}
}
func SwipedLeft(swipe : UISwipeGestureRecognizer){
if currentSelectedView == 0 {
//LoadSignupView()
}
}
func LoadLoginView(){
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
self.signupView.transform = CGAffineTransform(translationX : self.loginView.frame.size.width, y : 0)
self.loginView.transform = .identity
self.currentSelectedView = 0
})
}
func LoadSignupView(){
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
self.signupView.transform = .identity
self.loginView.transform = CGAffineTransform(translationX: self.loginView.frame.size.width, y: 0)
self.currentSelectedView = 1
})
}
}
Screenshot of my code and error
您传递的是 Int
,但所需的实际类型是 CustomSegmentedControl
。要简单地解决这个问题,只需为您的 CustomSegmentedControl
创建 IBOutlet
并将其作为参数传递给 Button_CustomSegmentValueChanged
方法。
func SwipedRight(swipe : UISwipeGestureRecognizer){
if currentSelectedView == 1 {
customSegmentOutlet.selectedSegmentIndex = 0
Button_CustomSegmentValueChanged(customSegmentOutlet)
//LoadLoginView()
}
}
我建议使用 CredentialSegmentedController 上的按钮数组来模仿更改段的行为
- 从您的自定义细分控件创建一个出口
然后使用下面的段控件修改您的代码
func SwipedRight(swipe : UISwipeGestureRecognizer) { if currentSelectedView == 1 { //Button_CustomSegmentValueChanged(0) //LoadLoginView() let btn = customSegmentOutlet.buttons[0] customSegmentOutlet.buttonTapped(btn) } } func SwipedLeft(swipe : UISwipeGestureRecognizer){ if currentSelectedView == 0 { //LoadSignupView() let btn = customSegmentOutlet.buttons[1] customSegmentOutlet.buttonTapped(btn) } }
假设你的CustomSegmentedControl
是UISegmentedControl
的子类,我修改了几行代码
@IBAction func button_CustomSegmentValueChanged(_ sender: UISegmentedControl?) {
// guard sender for nil before use
}
并且调用这个时
func swipedRight(swipe : UISwipeGestureRecognizer){
if currentSelectedView == 1 {
let seg = UISegmentedControl()
seg.selectedSegmentIndex = 0
Button_CustomSegmentValueChanged(seg)
}
}
如果 CustomSegmentedControl
不是 UISegmentedControl
的子类,请更改它。