在swift中验证文本输入

时间:2017-08-06 02:46:28

标签: ios swift xcode validation

我的代码不起作用,你能指出什么是错的。我正在尝试验证SNameInput中的用户输入,错误标签应该在输入无效时更改它的文本,当用户在SNameInput中输入并且它有效时,STitle应该更改它的文本,谢谢!

* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}

body {
  font-family: 'Roboto', sans-serif;
  font-weight: 300;
}

.nav {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 9;
  padding: 40px;
  color: white;
}
.nav h1 {
  font-weight: 300;
  font-size: 3rem;
}
.nav .author {
  text-align: right;
}

.loading {
  background-color: #2ecc71;
  width: 100%;
  position: absolute;
  top: 0;
  left: 0;
  height: 600px;
  line-height: 600px;
  text-align: center;
  color: white;
  font-size: 2rem;
}

.slider {
  background-color: white;
  position: relative;
  width: 100%;
  height: 600px;
  overflow: hidden;
  display: none;
}

.slider-inner {
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  -webkit-transition-timing-function: cubic-bezier(0.22, 1.61, 0.65, 1);
		  transition-timing-function: cubic-bezier(0.22, 1.61, 0.65, 1);
  -webkit-transition-duration: 1s;
		  transition-duration: 1s;
  background-visibility: hidden;
  -webkit-transition-delay: .75s;
		  transition-delay: .75s;
  -webkit-transform: translateZ(0);
		  transform: translateZ(0);
}

.slide {
  position: absolute;
  top: 0;
  height: 100%;
  background-color: #f1c40f;
  background-visibility: hidden;
  -webkit-transition-timing-function: cubic-bezier(0.25, 0.5, 0.25, 1.25);
		  transition-timing-function: cubic-bezier(0.25, 0.5, 0.25, 1.25);
  -webkit-transform: translateZ(0) scale(0.8, 0.8);
		  transform: translateZ(0) scale(0.8, 0.8);
  -webkit-transition-duration: .5s;
		  transition-duration: .5s;
  text-align: center;
  line-height: 600px;
  font-size: 5rem;
  color: white;
}
.slide.active {
  -webkit-transform: scale(1, 1);
		  transform: scale(1, 1);
  -webkit-transition-delay: 2s;
		  transition-delay: 2s;
}
.slide:nth-child(2n) {
  background-color: #2ecc71;
}
.slide:nth-child(3n) {
  background-color: #3498db;
}
.slide:nth-child(4n) {
  background-color: #9b50ba;
}

.slider-nav {
  position: absolute;
  bottom: 0;
  left: 50%;
  -webkit-transform: translateX(-50%);
		  transform: translateX(-50%);
  padding: 20px;
  text-align: center;
}
.slider-nav > div {
  float: left;
  width: 10px;
  height: 10px;
  border: 1px solid white;
  z-index: 2;
  display: inline-block;
  margin: 0 10px;
  cursor: pointer;
  border-radius: 50%;
  opacity: .5;
  -webkit-transition-duration: .25s;
		  transition-duration: .25s;
  background-color: transparent;
}
.slider-nav > div:hover {
  opacity: 1;
}
.slider-nav > div.active {
  background-color: white;
  -webkit-transform: scale(2);
		  transform: scale(2);
  opacity: 1;
}

1 个答案:

答案 0 :(得分:2)

可能性:您没有在editingDidEnd文字字段中添加SNameInput操作。

  

editingDidEnd:结束UITextField中的编辑会话   离开它的边界。

override func viewDidLoad() {
    super.viewDidLoad()

    SNameInput.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingDidEnd)
}

@IBAction func textFieldDidChange(_ sender: UITextField) {
    guard let string = sender.text else { return }

    if (string.isEmpty || string.characters.count >= 21) {
        errorLabel.text = "Name has to be a t least 1 character and not longer than 20"
    }
    else{
        errorLabel.text = ""
        Stitle.text = string
    }
}