初學者Swift程序員在這裏和新的堆棧溢出。在某些textFields或stackViews內以編程方式將UIButton更改爲UILabel?
我想做一個簡單的任務列表/ toDoPlanner,有點類似於iPhone上的Apple提醒應用程序。
我的目標是針對這個特定的問題是,當用戶點擊某個textField內的UIButton(這是一個stackView內)時,該UIButton應該更改爲UILabel。
下面是我在用戶完成輸入任務後出現的按鈕。這工作得很好:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
if(textField == textField1) // if the first text field is pressed
{
let image = UIImage(named: "To Be Completed Circle.png")
button = UIButton(frame: CGRect(x: 340, y: 56, width: 30, height: 30));
button.setBackgroundImage(image, for: UIControlState.normal)
self.view.addSubview(button)
button.addTarget(self, action:#selector(self.pressed), for: .touchUpInside)
}
...
然後在這裏是我正在試圖改變UIButton的,當用戶點擊它來改變該按鈕進入一個UILabel:
func pressed(textField: UITextField!) {
let image = UIImage(named: "Checkmark.png")
if(textField == textField1)
{
button.removeFromSuperview()
let label = UILabel(frame: CGRect(x : 340, y :56, width: 30, height: 30))
label.backgroundColor = UIColor(patternImage: image!)
self.view.addSubview(label)
}
整個if塊不正在閱讀,我不太確定爲什麼。我假設它必須處理UIButton和UITextField的單獨屬性。
任何幫助表示讚賞和任何其他技巧在iOS編程是有幫助的!
讓我知道你是否需要更多的細節。
你可以很容易地只做button.isEnabled = false來達到類似的效果。或相應地隱藏並顯示按鈕和/或標籤。兩個選項都應該有效。 – rMickeyD
我想要做的是用戶在第一個文本字段中輸入「任務」。按回車後,任務旁邊會出現一個灰色圓圈(按鈕)。我希望用戶能夠點擊這個按鈕,將灰色圓圈變成帶有紅色複選標記(標籤)的灰色圓圈,表示任務已完成。這是否澄清了我想要做的事情? – brownmamba
我會保持一個按鈕,並改變與按鈕相關的圖像。這種方式每次他們點擊按鈕時,它會在選中和未選中之間切換。 – rMickeyD