Next and Previous Page with Labels

Dears,


I've an array with 35 elements.

The values of my array is shown on 10 UILabels I have on my UIView.


So, once I have 35 elements, to be shown on 10 UILabels, it's evident that we can not show all elements together.

I would like to create a Next and Previus button to move from page to page...


So, page 1 will show the elements 0...9, page 2 will show the elements 10...19, page3 from 20...29 and page4 from 30...35


How can I do it?


Thank you so much.

Accepted Reply

You would so have an IBOutlet collection, with 10 labels, declared as

@IBOutlet labels : [UILabel]!

When you load the view it is populated with the first 10 elements of the array of 35 strings (I call it stringArray)

for i in 0...9 {
    labels[i].text = stringArray[i]
}

then in the next button, you increment by 10


Declare a var in your view :

var firstItem = 0

In the IBAction of next:


@IBAction showNext(_ sender: UIButton) {
     if firstItem >= stringArray.count - 10 { sender.isEnabled = false }    // You will enable with prev button
          // Adapt as you like:you could also reset value as firstItem = stringArray.count - 1
     firstItem += 10
     for i in 0...9 {
          if firstItem + i < stringArray.count {
              labels[i].text = stringArray[firstItem + i]  
          } else {
               labels[i].text = "-"
          }
     }
}

Replies

Paging with Next and Prev is not a preferred way in native iOS aps.


Have you considered using UITableView, UICollectionView or UIPageViewController ?

I can't do it because I'm creating something like a machine's simulator. So I'll have to replicate exactly the way the real machine works.


I want to have a next button... just to LOOP from the 0 to 9, then 10 to 19, 20 to 29... and so son...


the bext button... it's and image and will not show another UIVIew... it will only loop to the next groupf of labels...


Did u get it?

You say can't do it for UIPageViewController and in other place you say how can I see it?


Which is right? You said I can't do it without knowing what UIPageViewController is?

You would so have an IBOutlet collection, with 10 labels, declared as

@IBOutlet labels : [UILabel]!

When you load the view it is populated with the first 10 elements of the array of 35 strings (I call it stringArray)

for i in 0...9 {
    labels[i].text = stringArray[i]
}

then in the next button, you increment by 10


Declare a var in your view :

var firstItem = 0

In the IBAction of next:


@IBAction showNext(_ sender: UIButton) {
     if firstItem >= stringArray.count - 10 { sender.isEnabled = false }    // You will enable with prev button
          // Adapt as you like:you could also reset value as firstItem = stringArray.count - 1
     firstItem += 10
     for i in 0...9 {
          if firstItem + i < stringArray.count {
              labels[i].text = stringArray[firstItem + i]  
          } else {
               labels[i].text = "-"
          }
     }
}

That's perfect

Thank you again Claude

Claude...

I'm sorry to ask you a lot of questions... But I'm learning a lot with you!


Is it possible to "convert" the code you provided above, to a function?

Which code ? this one ?


@IBAction showNext(_ sender: UIButton) {
     if firstItem >= stringArray.count - 10 { sender.isEnabled = false }    // You will enable with prev button
          // Adapt as you like:you could also reset value as firstItem = stringArray.count - 1
     firstItem += 10
     for i in 0...9 {
          if firstItem + i < stringArray.count {
              labels[i].text = stringArray[firstItem + i]
          } else {
               labels[i].text = "-"
          }
     }
}

Do you want to be able to call this as a func, not only as an IBAction ?


If I understand well, just create this func


func show10Next() {
     firstItem += 10
     for i in 0...9 {
          if firstItem + i < stringArray.count {
              labels[i].text = stringArray[firstItem + i]
          } else {
               labels[i].text = "-"
          }
     }
}

Now, you can call show10Next() anywhere, just like this


You can then change IBAction

@IBAction showNext(_ sender: UIButton) {
     if firstItem >= stringArray.count - 10 { sender.isEnabled = false }    // You will enable with prev button
     show10Next()
}



Is it what you were looking for ?

Thanks Claude...


It's not what I'm looking for.


The app I'm working with has 4 buttons. Each of this buttons will show a list of items on its UILabels.

This items came from 4 different arrays. Each of this array may have around 30 items.


But the app has only 12 UILabels, to be filled with the elements of an array.

The first element of each array is the pages title, so, the elements from 1 to 12 will be shown... but the others elements wont, once I have no UILabels for everybody.


I would like to create two buttons (PREV and NEXT), pressing the next button for example will "capture" the last used array, and show the elements from 13 to 25, and so on... until the end. If there is nothing more to be show, if the user press NEXT again, will return to the first elements of the list...


And, i would like to have a function, at the NextPrevious.swift file, and call this function at the viewController


Is it more "understandable" now???


Thank you