2
votes

I'm expecting to get a little help with a UIButtons staying .hidden. I'm new to this site so please give me a min to best describe this problem I face.

Below is a picture of 2 UIButtons, in the middle of these UIButtons there is another one called OnRoute. Once the Acknowledged button is pressed it is hidden to which sends a status and reveals the OnRoute UIButton. Now the Acknowledged button is hidden you will only see on screen under the Runsheet button the OnRoute button to which you also press that sends a status and then hides it self.

Once these buttons are pressed you are sent to a UITableView and at this point all is well, but when you go back to the menu screen the buttons are reappear as if the buttons have not been pressed. And you can repeat over and over sending status.

The idea of this is to send a job status once the buttons are pressed which in turn shows on software on a server. Once these have been sent and the UIButtons hide for that job number, I would like to keep them hides until job has gone from hand set.

This is complex problem but if anyone has any ideas of this, I would really be thankful.

//This is in ViewDidLoad

self.onroute.hidden = YES;
NSNumber *num = [NSNumber numberWithInt:10.00];

self.acknow.hidden = YES;

if((self.consignment.cur_status_no < num) || [self.consignment.newjob isEqual:@(YES)]){

    self.acknow.hidden = NO;

//This is in IBAction

- (IBAction)acknowledgebtn:(id)sender {


    if (self.onroute.hidden == YES){
        self.acknow.hidden = NO;
        self.onroute.hidden = NO;
        self.acknow.hidden = YES;

//and this is for the other IBAction

if (self.acknow.hidden == YES){
    self.onroute.hidden = YES;

As I'm new to the site it will not let me post picture of UIButton sorry for this.

2
When you come back to the menu screen, the buttons should still have the same hidden status as when you left -- if this is not happening, it's probably because you are creating a new instance of the menu controller instead of going back to the old done. How are you going back? - rdelmar

2 Answers

1
votes

My suggestion would be to use some booleans instead of relying on the buttons hidden property. Then save the booleans when transferring to a new view. Then when you return to the main menu check the booleans and see what should be hidden and what should not be.

Also when I name variables I like to pretend that someone else will be looking at my code. So instead of just onroute as the button name, I would make it onrouteBut. This makes it a lot easier when I go back through my code as well so I know exactly what each variable is just by looking at the name.

As for the code I don't know how you are presenting views, so I can't really give a full answer. But I think this will help.

in your .h

@property (nonatomic) BOOL onrouteBool;
@property (nonatomic) BOOL acknowBool;
//whatever other bools you need instead of using button.hidden == YES/NO

in your .m

@synthesize onrouteBool, acknowBool;

-(void)viewDidLoad {
    onrouteBut.hidden = YES;
    onrouteBool = YES;

    NSNumber *num = [NSNumber numberWithInt:10.00];

    acknowBut.hidden = YES;
    acknowBool = YES;

    if((self.consignment.cur_status_no < num) || [self.consignment.newjob isEqual:@(YES)]) {

        acknowBut.hidden = NO;
        acknowBool = NO;
     }

}

-(IBAction)acknowledgeBtn:(id)sender {

     if (onrouteBool == YES) { 

        acknowBut.hidden = NO; 
        onrouteBut.hidden = NO; 

        acknowBool = NO;
        onrouteBool = NO;

        //this part doesn't make sense you set the button to visible and then hidden right after
        acknowBut.hidden = YES;
        acknowBool = YES;
    }

}

-(IBAction)onrouteBtn:(id)sender {

    if (acknowBool == YES) {

    onrouteBut.hidden = YES;
    onrouteBool = YES;

    }

}

So now before you transition to your next view call this method to save the bools

-(void)saveTheBools {

    //save the bools however you want before you transition the view
    //one way is nsuserdefaults

    [[NSUserDefaults standardUserDefaults]setBool:onrouteBool forKey:@"onrouteBool"];
    [[NSUserDefaults standardUserDefaults]setBool:acknowBool forKey:@"acknowBool"];

    [[NSUserDefaults standardUserDefaults]synchronize];
    //how you save them

}

then when you transition back to the main menu check the bools to see if the buttons should be hidden

-(void)checkTheBools {

    onrouteBool = [[NSUserDefaults standardUserDefaults] boolForKey:@"onrouteBool"];
    acknowBool = [[NSUserDefaults standardUserDefaults] boolForKey:@"acknowBool"];

    if (onrouteBool == YES) {
         onrouteBut.hidden = YES;
    }
    else {
        onrouteBut.hidden = NO;
    }

    if (acknowBool == YES) {
        acknowBut.hidden = YES;
    }
    else {
        acknowBut.hidden = NO;
    }

    //whatever else you need to hidden or make visible
}

This is all just to give you some ideas of what to do. Use what you need to make it work. This is how I would do it, I don't know if this is best way to do it but it's a starting point. I can't really give a specific answer without seeing all of your code, since I don't know how you're transitioning views, what you are initializing, retaining, etc.

Hope this helps you out, if not my bad. Just keep working at it and you'll find something that works for you eventually.

edit:

As for the status problem you are having I can't really help because I don't have the code to look at. I think it probably has to do with saving your variables so you can access them across classes. So like I showed you how to save the booleans and use them you probably will have to do something similar to check if the status has sent or not.

I suggested using nsuserdefaults because that is the easiest thing to do, however it is not the best to rely on that for saving all of your variables. You can also look into singletons, core data, or anything that will allow you to save the variables that you need across classes. You just have to find the way that works best for what you are trying to do.

The only way you are going to learn is to struggle at times, do some research, and try different things until you find a solution. Also take advantage of the resources apple provides you with as a developer. I think you will be able to figure this one out. Good luck

1
votes

Just wanted to update anyone having this problem, I managed to fix this using doubleValue.

onroute.hidden = YES;
onrouteBool = YES;


NSNumber *num1 = [NSNumber numberWithDouble:10.00];

if(([self.consignment.cur_status_no doubleValue] < [num1 doubleValue]  ) ) {

    if([self.consignment.newjob isEqual:@(NO)]) {

        onroute.hidden = NO;
        onrouteBool = NO;
    }
}
acknow.hidden = YES;
acknowBool = YES;

if([self.consignment.newjob isEqual:@(YES)]) {

    acknow.hidden = NO;
    acknowBool = NO;
}

Thanks again for all your help.