Search code examples
iosobjective-cuiviewuiscrollviewuibutton

ios-Update multiple UIViews on UIScrollview


I have a ViewController that contain a UIScrollView, that contains a few views which are custom UIViews (very simple uiimage + uibutton) that the user can scroll between (one custom view at the a time). I want the user to be able to "mark" a photo, and then to display a certain text, when the user select a different photo (by using the button), I want to update the text on the previous selected photo and the current selected.

What should I do? Inside the view itself I don't have access to the previous view, I figured I should send a notification to the view controller but than I have no access to the button I want to update


Solution

  • You could use tags to identify the scrollViews subviews. Add a tag to the subview and the same tag to the button that belongs to the subview while adding them to the scrollView.

    view.tag = 1;
    button.tag = 1;
    

    Then in the method called by the buttons, you can use the buttons tag to get the according custom view.

    -(void)buttonClick:(id)sender{
       UIButton *btn = (UIButton*)sender;
       NSInteger tag = btn.tag;
       UIView* customView = [self.scrollView viewWithTag:tag];
    }
    

    To access the prevoius selected view, store the tag of that view when editing for the next call of the function.