Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Devinder Sodhi
Devinder Sodhi
5,493 Points

How to make an IOS Switch only clickable once a day

I m trying to add functionality to a switch in an Iphone APP in a custom TableViewCell. The big picture is that I m trying to get an offline mode working for a daily workout app.

The idea is this : you can set a switch to an on state. After you do this - you can no longer make changes until 12:00 o clock at midnight. There's an added level of complexity to this, because I wipe the Table Data every time I load this view - and have it pick up a new set of stats from an API. I probably have to end up keeping some of this data in a separate entity, but the main entity gets wiped everytime the call is made.

Here's how i imagine it works.

On Table View Load, The Switch has to Look at my CoreData entity for a bool value. If that that value is set to true, then it sets it self to "ON" state. So really its a two part question:

  1. how do i make my switch able to disable only itself (and not the entire tableview Cell)

  2. How do I make Core Data able to automatically switch Bool values to off for a certain entity at midnight? (I also need to increment a value within my entity each time this happens)

Any Aid would be MOST welcome. PS: loving the tutorials

3 Answers

Where is the switch? On each cell or head/footer of table?

1.) After you retrieve the bool value from Core Data, Im assuming you have a custom cell so..

if(yourBOOL){
cell.yourSwitch.alpha = 0;  //hide
}else{
cell.yourSwitch.alpha = 1;  //show
}

Using alpha does not technically disable it. It just makes it non-visible to the user & actions.

That way the switch will be hidden on any/all the cells. If your switch is not on a cell, then you can just use yourSwitch.alpha = 0;

2.) Every time the user presses the switch, save the date/time value somewhere else for later use, maybe Core data or NSUserDefaults.

You can then check to see how much time has passed since they pressed it

`NSTimeInterval howMuch= [yourSavedDate timeIntervalSinceDate:currentData];  

if the 24hours has passed then you do whatever you need to.

Devinder Sodhi
Devinder Sodhi
5,493 Points

The Switch sits on each individual Cell,

Interesting, I'll look into NSTimeInterval. i think I might be happy with just 12 o clock user time as the reset time, rather than a difference in time.

This give me a good place to start though, Thanks!

I thought Maybe my switch'es IBAction should contain some of the code for it being enabled or not. But i think i had that inside out. The cell should determine switch behavior and then the behaviour upon switch click should maybe just do exactly what it is that needs to be done upon a switch click.

I will try and make this code happen, thanks for the hints!

No problem. Let me know how it goes!

Devinder Sodhi
Devinder Sodhi
5,493 Points

will do - API is on a local server, will have access to it again tomorrow. Trying to write the code around it now. Really started IOS like 9 days ago so a lota checkbacks. Will probably be slower than you expect - i'll likely know by tomorrow evening eastern time.

Devinder Sodhi
Devinder Sodhi
5,493 Points

i got side tracked with other parts of the app, had like 8 other api calls that were finicky, finally this is getting implemented tomorrow. Its gonna be awesome