как изменить значение int в тексте метки каждый раз при нажатии кнопки - PullRequest
0 голосов
/ 24 марта 2019

// ЗДЕСЬ КОД, КОТОРЫЙ Я ТАК ИСПОЛЬЗУЛ.

var cellIndex: Int = 0
override func viewDidLoad() {
    super.viewDidLoad()
    cellIndex = 1
    let s1 = "word \(cellIndex) to \(cellIndex+7)"
    let mystring:NSString = "\(s1) recovery phrases" as NSString
    var myMutableString = NSMutableAttributedString()
    myMutableString =  NSMutableAttributedString(string:mystring as String, attributes: [NSAttributedString.Key.font:Font.mediumRegularFont()])
    myMutableString.addAttribute(NSAttributedString.Key.foregroundColor, value: Color.darkColor(), range: NSRange(location:0,length:s1.count))
    stepBarView.attributedText = myMutableString
}
@IBAction func onNextButtonTap(_ sender: Any) {
    // i want to change cellIndex Value every time when i press button.
}

Ответы [ 2 ]

0 голосов
/ 24 марта 2019

Вы можете использовать didSet внутри cellIndex.

var cellIndex: Int = 0 {
    didSet {
        let s1 = "word \(cellIndex) to \(cellIndex+7)"
        let mystring:NSString = "\(s1) recovery phrases" as NSString
        var myMutableString = NSMutableAttributedString()
        myMutableString =  NSMutableAttributedString(string:mystring as String, attributes: [NSAttributedString.Key.font:Font.mediumRegularFont()])
       myMutableString.addAttribute(NSAttributedString.Key.foregroundColor, value: Color.darkColor(), range: NSRange(location:0,length:s1.count))
        stepBarView.attributedText = myMutableString
    }
} 

Теперь, когда значение установлено в cellIndex, оно будет обновлять метку нужной строкой.

0 голосов
/ 24 марта 2019

У вас есть 2 варианта

1 замените значение на replacecingOccursions , но необходимо учитывать, сколько цифр может иметь число. 2 воссоздайте всю строку снова, что быстрее и требует меньше усилий

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...