0
votes

I'm making an app. My app is based off this sample project. https://github.com/yeahdongcn/RSCircaPageControl

In the app there is a view. On the view, there is a scroll view (UIScrollView *scrollView) which allows to 10 "pages" of content. Within each page, there is another scroll view (UIScrollView *sv) which is a subview of *scrollView.

On SV, I then added a button called UIButton *button. When I attempt to change the button's images after a time of "two minutes," nothing happens. This is because there are 10 buttons. I need to change the image of the button on only one of the pages. Not all of them.

Something tells me it needs "object at index" or something to specify which button on which page I want to change.

The code is below! Thanks guys!

- (void)viewDidLoad
{
[super viewDidLoad];

self.clipView = [[RSView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
self.clipView.clipsToBounds = YES;
[self.view addSubview:self.clipView];

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.clipView.bounds.size.width, kScrollViewHeight)];
self.scrollView.delegate = self;
self.scrollView.clipsToBounds = NO;
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:self.scrollView];
self.clipView.scrollView = self.scrollView;

self.pageControl = [[RSCircaPageControl alloc] initWithNumberOfPages:10];
CGRect frame = self.pageControl.frame;
frame.origin.x = self.view.bounds.size.width - frame.size.width - 10;
frame.origin.y = roundf((self.view.bounds.size.height - frame.size.height) / 2.);
self.pageControl.frame = frame;
self.pageControl.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[self.pageControl setCurrentPage:0 usingScroller:NO];
[self.view addSubview:self.pageControl];

CGFloat currentY = 0;
for (int i = 0; i < 10; i++) {
    UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, currentY, self.scrollView.bounds.size.width, kScrollViewHeight)];
    sv.tag = kScrollViewTagBase + i;
    sv.delegate = self;
    sv.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    sv.backgroundColor = [UIColor colorWithRed:(arc4random() % 257) / 256. green:(arc4random() % 257) / 256. blue:(arc4random() % 257) / 256. alpha:1];



   ///here's the buttton!!


   UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
   UIImage*normal = [UIImage imageNamed:@"girl.jpg"];
  UIImage*selected = [UIImage imageNamed:@"girl2.jpg"];
    button.frame = CGRectMake(100, 100, 50,50));  
    [button setImage:normal forState:UIControlStateNormal];
    [button setImage:selected forState:UIControlStateHighlighted];
    button.contentMode = UIViewContentModeScaleAspectFit;
    [button addTarget:self
               action:@selector(myButtonPressed:)
     forControlEvents:UIControlEventTouchDown];
    [sv addSubview:button];    //notice that the button is added as a subview of SV

    [self.scrollView addSubview:sv];  // notice that SV is a subview of the scrollView.


    if (i == 2 || i == 6) {
        sv.contentSize = CGSizeMake(sv.contentSize.width, kScrollViewContentHeight);
    }



    currentY += kScrollViewHeight;
}

self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, currentY);

  [self performSelector:changeButtomImages withObject:nil afterDelay:TwoMinutes];

}

- (void)changeButtonImages {

    UIImage*normal2 = [UIImage imageNamed:@"boy.jpg"];
    UIImage*selected2 = [UIImage imageNamed:@"boy2.jpg"];
    [button setImage:normal2 forState:UIControlStateNormal];
    [button setImage:selected2 forState:UIControlStateHighlighted];
} 

Kyle

2

2 Answers

0
votes

for setting button image in a normal state the parameter to pass forState: is UIControlStateNormal not UIControlStateHighlighted

0
votes

So, in changeButtonImages you are changing the the image for the iVar "button", but you are never actually assigning anything to that button var. In your for loop in viewDidLoad, you are creating a local variable of UIButton called button. Maybe something like this?

- (void)myButtonPressed:(id)sender 
{
    UIButton *button = (UIButton *)sender;
    UIImage*normal2 = [UIImage imageNamed:@"boy.jpg"];
    UIImage*selected2 = [UIImage imageNamed:@"boy2.jpg"];
    [button setImage:normal2 forState:UIControlStateNormal];
    [button setImage:selected2 forState:UIControlStateHighlighted];
}