2016-12-16 56 views
0

單擊時如何更改標籤中的字母? 2個按鈕用於較小的字母或大寫。單擊時如何更改標籤中的字母?

當我們單擊小寫字母的標籤更改爲小寫字母,反之亦然。

如何做到這一點?我嘗試了一些代碼,但我被卡住了。

enter image description here

let ABC = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"] 

var str: Character = "Z" 

//@IBOutlet weak var capitalABC: UIButton! 
// @IBOutlet weak var smallac: UIButton! 
// @IBOutlet weak var Abclbl: UILabel! 
@IBOutlet weak var Abclbl: UILabel! 
// @IBOutlet weak var Abclbl: Le! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    Abclbl.text = ABC[0] 

    // Do any additional setup after loading the view. 
} 

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

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

請澄清你的問題。您發佈的代碼到底有什麼問題? – rmaddy

+0

是的。這個問題並不能清楚地理解 –

+0

聽起來像你的任務的一部分涉及改變一個字符串的情況。 'String'具有['小寫()'](https://developer.apple.com/reference/swift/string/1641392-lowercased)和['大寫()'](https://developer.apple。 com/reference/swift/string/1640996-uppercased)函數。 – mattliu

回答

0

有小後者與資本後者之間的差異,當你嘗試喲得到像inde = ABC.indexOf("a")指數將返回但是當你試圖讓這種方式inde = ABC.indexOf("A")它將返回 我對你的功能有點變化和該功能處理小型和資本後兩個功能 試試這個 首先,你需要添加一個標誌變量,它處理的按鈕PRES S(小或資本) 像這樣

var isCapital = true 

添加上述變量的功能,例如波紋管

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!) 
    } 
} 
+0

Actuallu任務是當我們點擊小按鈕,我們得到小alphapets的標籤,當我們點擊預定按鈕,它獲得(F,E,d,C,B,A),我們點擊下一步按鈕,我們得到(A,B,C, d,e,f)如果我們點擊資本按鈕,我們可以得到大寫字母。 –

+0

你還沒有加入資本與小按鈕操作請加兩個動作的話,我會更新我的答案 –

+0

我已經更新了我的answer.you可以籤我更新的答案。 –

0

如果需要簡單的文本轉換爲小寫和大寫在前然後轉換您的字符串鍵後點擊方法像波紋管

爲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" 
}