Uba Duba
Uba Duba

Reputation: 241

Dynamically updating buttons on a view in IOS?

I hope I can ask this question clearly, I have a viewController that I build programmatically (not with NIB), this view controller has two buttons I draw on the lower portion of the view

"prev" and "next" what I'd like to do is, when I've reached the "end" I'd like to only draw the "prev" button, and not the "next", and vice-versa, when I'm at the beginning, I'd like to only draw the "next" and not the prev.

Any general ideas on how to approach this ?

Thanks,

uba

Upvotes: 0

Views: 136

Answers (2)

LostPuppy
LostPuppy

Reputation: 2501

The first thing to do is to addTarget for your button to listen to touch events

[yourButtonNameHere addTarget:self action:@selector(yourCallBackFunctionNameHere:) forControlEvents:UIControlEventTouchUpInside];

What this does is whenever your button is pressed it calls the yourCallBackFunctionNameHere function. Do this for the other button too. The semi colon after the function indicates that the function has to send the information of the UIElement that caused the event to occur.

Assign tags to both the buttons.

youButtonNameHere.tag=yourTag;

In the function check which button is sending the UIcontrolEvent by comparing the tags

- (void)yourFunctionNameHere:(id)sender {
   UIButton *yourButton =(UIButton *)sender;

    if(yourButton.tag==501){

      // logic to check if this is the first or last page and act accordingly
       yourButton.hidden = YES / NO based on what you want to do.
     }else{  
     // logic to do otherwise.
     }

Upvotes: 1

Gui Del Frate
Gui Del Frate

Reputation: 691

You can use "hidden" property of UIButton:

if (page == firstPage) {
    self.myButtonPrev.hidden = YES;
} else {
    self.myButtonPrev.hidden = NO;
}

if (page == lastPage) {
    self.myButtonNext.hidden = YES;
} else {
    self.myButtonNext.hidden = NO;
}

Upvotes: 1

Related Questions