Refreshing a view

拟墨画扇 提交于 2019-12-13 04:21:06

问题


Okay I have created an application where in one of the screens I have a call slot screen. Basically there are 18 buttons, ranging from 09.00-09.30 all the up up to 17.30-16.00 split up into two buttons per row. There are also two arrows at the top, allowing the user to go forward up to seven days and back to current day.

When a user clicks one of the buttons, it will pop up with an alert stating the time selected.

Now what I want is, when the user accesses this view, any timeslot that is not available to be changed to have a different image.

I have created the buttons like this

    for(int i = 0; i <[imageList count]; i++){
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(xPos, yPos, 145, 55)];
    csBut = [[UIButton alloc]init];
    csBut = [UIButton buttonWithType:UIButtonTypeCustom];
    csBut.frame = CGRectMake(0, 0, 145, 55);
    [csBut addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [csBut setImage:[imageList objectAtIndex:i] forState:UIControlStateNormal];
    csBut.tag = (100 + i);
    [myView addSubview:csBut];
    [SV  addSubview:myView];

    if(i % 2 == 0){
        xPos += 160;    
    }else{
        yPos+=60; 
        xPos = 5;
    }

} 

[self emptySlots];

}

Now the empty slots method declared at the bottom, is a method which scans each timeslot and checks if the hour is less than the current hour. If it is, then image is changed to a the faded out button image. Code is

-(void)emptySlots{

int tmpValue = 100;
int myInteger = 1;
int tmpHour = 9;
int tmpMinute = 0;

for(int i = 0 ; i <18; i++){
    UIButton *validSlot = (UIButton*)[self.view viewWithTag:tmpValue];

    if(i % 2 == 0){

        tmpMinute = (arc4random() % 30);
    }else{

        tmpMinute = (arc4random() % 30) + 30;
        tmpHour+=1;

    }

    [self checkValidSlot:tmpHour :tmpMinute];

    if(tslot){
        NSString* myNewString = [NSString stringWithFormat:@"tb%i.png", myInteger];    
        [validSlot setImage:[UIImage imageNamed:myNewString] forState:UIControlStateSelected];
        [validSlot setSelected:YES]; 
    }else{
        NSString* myNewString = [NSString stringWithFormat:@"fb%i.png", myInteger];    
        [validSlot setImage:[UIImage imageNamed:myNewString] forState:UIControlStateSelected];
        [validSlot setSelected:YES]; 
    }

    tmpValue+=1;
    myInteger+=1;
}

}

The empty slots code is not perfect yet, still few things to fix, but not the issue I am having.

The problem I am having is when the users click the right arrow, to go to the next day, it runs code called fillslots, which automatically refills in the slots with the normal buttons as the next day the timeslots will be available. The problem is it does not refresh straight away. Same with when the users click back arrow to go back to the current day and runs the empty slots method, I have to click the arrow again before it refreshes.

How do I make it so when the user clicks the arrow, it automatically refreshes the view so I don't have to click the arrow again as its rather frustrating and I can't figure it out :@

Thanks in advance for any help. Hope I was clear enough


回答1:


Try the setNeedsDisplay of UIView. For more reference, visit Apple documentation

It would be something like [SV setNeedsDisplay]; in the button click action method last line.




回答2:


I managed to figure it out. I used threads to update the callslots and it worked perfectly :D



来源:https://stackoverflow.com/questions/8742099/refreshing-a-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!