如何通过按下按钮遍历数组
How to go through array with button press
我有一个包含不同信息的数组,
如何通过按下按钮遍历数组?我有两个按钮,我需要让其中一个在数组中向前移动,让后退按钮显示上一个索引。
@IBOutlet weak var backButton: UIButton!
@IBOutlet weak var nextButton: UIButton!
@IBOutlet weak var infoLabel: UILabel!
@IBOutlet weak var pageControl: UIPageControl!
Let infoArray = ["info1","info2","info3","info4"]
@IBAction func nextTapped(_ sender: Any) {
// Change the label based on the selected index in array
}
@IBAction func backTapped(_ sender: Any) {
//Return to previous index and update label text
}
我还添加了页面控件以获得更好的用户体验,但现在我只是担心学习如何通过点击按钮更改标签。
我的猜测是从 0 开始索引,也就是 info1
然后从那里开始。我可以担心稍后保存索引状态。
非常感谢任何帮助。
逻辑应该是这样的
let infoArray = ["info1","info2","info3","info4"]
func viewDidLoad() {
pageControl.numberOfPages = infoArray.count
pageControl.currentPage = 0
}
@IBAction func nextTapped(_ sender: Any) {
// Change the label based on the selected index in array
guard pageControl.currentPage + 1 < infoArray.count else {
return
}
pageControl.currentPage += 1
}
@IBAction func backTapped(_ sender: Any) {
//Return to previous index and update label text
guard pageControl.currentPage - 1 >= 0 else {
return
}
pageControl.currentPage -= 1
}
When a user taps a page control to move to the next or previous page, the control sends the valueChanged event for handling by the delegate. The delegate can then evaluate the currentPage property to determine the page to display. The page control advances only one page in either direction.
请检查下面的代码,你可以做这样的事情来向前和向后移动你的数组。请根据需要添加其他检查。
var i = 0
let infoArray = ["info1","info2","info3","info4"]
@IBAction func nextTapped(_ sender: Any) {
// Change the label based on the selected index in array
if i >= 0 && i < infoArray.count{
dataLbl.text = infoArray[i]
}
if i > 3 {
i = i-1
} else {
i = i+1
}
}
@IBAction func backTapped(_ sender: Any) {
//Return to previous index and update label text
if i >= 0 && i < infoArray.count-1 {
dataLbl.text = infoArray[i]
}
if i < 0 {
i = i+1
} else {
i = i-1
}
}
我有一个包含不同信息的数组,
如何通过按下按钮遍历数组?我有两个按钮,我需要让其中一个在数组中向前移动,让后退按钮显示上一个索引。
@IBOutlet weak var backButton: UIButton!
@IBOutlet weak var nextButton: UIButton!
@IBOutlet weak var infoLabel: UILabel!
@IBOutlet weak var pageControl: UIPageControl!
Let infoArray = ["info1","info2","info3","info4"]
@IBAction func nextTapped(_ sender: Any) {
// Change the label based on the selected index in array
}
@IBAction func backTapped(_ sender: Any) {
//Return to previous index and update label text
}
我还添加了页面控件以获得更好的用户体验,但现在我只是担心学习如何通过点击按钮更改标签。
我的猜测是从 0 开始索引,也就是 info1
然后从那里开始。我可以担心稍后保存索引状态。
非常感谢任何帮助。
逻辑应该是这样的
let infoArray = ["info1","info2","info3","info4"]
func viewDidLoad() {
pageControl.numberOfPages = infoArray.count
pageControl.currentPage = 0
}
@IBAction func nextTapped(_ sender: Any) {
// Change the label based on the selected index in array
guard pageControl.currentPage + 1 < infoArray.count else {
return
}
pageControl.currentPage += 1
}
@IBAction func backTapped(_ sender: Any) {
//Return to previous index and update label text
guard pageControl.currentPage - 1 >= 0 else {
return
}
pageControl.currentPage -= 1
}
When a user taps a page control to move to the next or previous page, the control sends the valueChanged event for handling by the delegate. The delegate can then evaluate the currentPage property to determine the page to display. The page control advances only one page in either direction.
请检查下面的代码,你可以做这样的事情来向前和向后移动你的数组。请根据需要添加其他检查。
var i = 0
let infoArray = ["info1","info2","info3","info4"]
@IBAction func nextTapped(_ sender: Any) {
// Change the label based on the selected index in array
if i >= 0 && i < infoArray.count{
dataLbl.text = infoArray[i]
}
if i > 3 {
i = i-1
} else {
i = i+1
}
}
@IBAction func backTapped(_ sender: Any) {
//Return to previous index and update label text
if i >= 0 && i < infoArray.count-1 {
dataLbl.text = infoArray[i]
}
if i < 0 {
i = i+1
} else {
i = i-1
}
}