单击时如何更改标签中的字母?

时间:2016-12-16 04:21:00

标签: ios swift

单击时如何更改标签中的字母? 2个小写字母或大写字母的按钮。

当我们点击小写字母时,标签会变为小写字母,反之亦然。

怎么做?我尝试了一些代码但是我被卡住了。

enter image description here

 YourObject obj = getIntent().getParcelableExtra("key");
 //Your case
GetDataAdapter obj = getIntent().getParcelableExtra("key");

2 个答案:

答案 0 :(得分:0)

当你尝试获得类似inde = ABC.indexOf("a")的索引时,小后者和后者之间存在差异,它会返回 nil 但是当你试图这样做时inde = ABC.indexOf("A")它将返回 0 我的功能稍有变化,这个功能稍后处理Small And Capital两个功能 试试这个 首先你需要添加一个标志变量来处理按哪个按钮(小或大写) 像这样

var isCapital = true

将以上变量添加到您的功能之后,如bellow

func ABCCapital(sender: AnyObject) {
    isCapital = true
    let currentValue = self.Abclbl.text!
    Abclbl.text = currentValue.capitalized

}
func ABCSmall(sender: AnyObject) {
    isCapital = false
    let currentValue = self.Abclbl.text!
    Abclbl.text = currentValue.lowercaseString

}

func ABCPre(sender: AnyObject) {
    if Abclbl.text == "a" || Abclbl.text == "A"
    {
        print("Do nothing")
    }
    else
    {
        let preVal = self.Abclbl.text!
        if isCapital == true {
            var inde = ABC.indexOf(preVal)
            inde = inde! - 1
            let get = ABC[inde!]
            Abclbl.text = get
        } else {
            var inde = ABC.indexOf(preVal.capitalized)
            inde = inde! - 1
            let get = ABC[inde!]
            Abclbl.text = get.lowercaseString
        }
        alphReocg(Abclbl.text!)
    }
}

func ABCNext(sender: AnyObject) {
    //let str: String = "Z"
    if Abclbl.text! == "Z" || Abclbl.text! == "z"
    {
        print("Do nothing")
    }
    else
    {
        let preVal = self.Abclbl.text!
        if isCapital == true {
            var inde = ABC.indexOf(preVal)
            inde = inde! + 1
            let get = ABC[inde!]
            Abclbl.text = get
        } else {
            var inde = ABC.indexOf(preVal.capitalized)
            inde = inde! + 1
            let get = ABC[inde!]
            Abclbl.text = get.lowercaseString
        }

        alphReocg(Abclbl.text!)
    }
}

答案 1 :(得分:0)

如果你需要简单地将文本转换为小写和upercase,然后在按钮点击方法中转换你的字符串,如bellow

用于swift 3.0首次导入基础

import Foundation

然后在按钮点击操作

 func uppercase(sender: AnyObject) {

    var title = "Text That you need to convert " 
    title.uppercased() // "TEXT THAT YOU NEED TO CONVERT"
}

对于小写

   func lowercase(sender: AnyObject) {

    var title = "Text That you need to convert" 
    title.lowercased() // "text that you need to convert"
}