0

I want to implement a function whereby a lock button is pressed which locks the current state of my app and prevents all scrolling and button pressing until the unlock button is pressed. I have no idea how to go about this please give me ideas and sample code! There is a similar function in the app GoodReader if anyone knows of this.

Thanks!

Adedayo
  • 73
  • 8
  • [This](http://stackoverflow.com/questions/5404856/how-to-disable-touch-input-to-all-views-except-the-top-most-view) might give you some ideas. – Krishnabhadra Sep 04 '14 at 11:50

2 Answers2

0

try something like:

for(UIView* subview in [self.view subviews])
    if(subview != yourLockButton)
        subview.userInteractionEnabled = NO;
  • Hi, where should I put this code and what is it doing? – Adedayo Sep 04 '14 at 12:45
  • in a method. If you want to do it when you tap a button you should do this in the button's action. The method iterates over all the subviews and disables the user interaction in them, except in the lockbutton –  Sep 04 '14 at 12:47
  • @Ade note that if you have an scrollview maybe you also should iterate its subviews –  Sep 04 '14 at 13:11
  • where you wrote "yourLockButton" what do I write. I have made a IBAction for the lock button pressed called "lockButton" but do I need to make an IBOutlet and write this here or what? Thanks – Adedayo Sep 04 '14 at 14:42
  • I created a property and it works! Thankyou very much! – Adedayo Sep 04 '14 at 14:45
  • Maybe you could help me with the next part instead of me posting another question. How do I change the lock button to an unlock button once the app is locked? rather than have two separate buttons – Adedayo Sep 04 '14 at 14:46
  • @Ade sorry, i couldnt connect before. About yourLockButton, its the name of the instance of the button. About changing the button, its easy, you only have to change its text (if you have an image that you wanna change, change it too). If i dont remember bad, this code should be fine for that: [yourLockButton setTitle:@"new text" forState:UIControlStateNormal]; You should also put a flag var to switch between user interaction and button text –  Sep 04 '14 at 23:47
  • o Pi I have included that code and it changes the text. However I want to change it to "Unlock" but it seems it is too long to fit because it comes up as "..." when I launch the app but "Hi" seems to show up fine. Also where would I now put the code that will unlock the locked subviews once the "Unlock" is pressed? Muchas gracias! – Adedayo Sep 05 '14 at 18:50
  • @Ade you can use the same method for both actions. For example, if the text of the button is "lock" you'll set the user interaction to no and change the text to unlock. If the button text is "unlock" you'll set uset interaction to yes and change the text to lock. De nada ;) –  Sep 05 '14 at 23:23
  • I cannot do mylockbutton.text though, it gives an error 'Property 'text' not found on object of type UIButton *' – Adedayo Sep 06 '14 at 00:27
  • please bear in mind i am using UIBarButtonItems on some views for the lock button so I can use .title instead of .text but forState:UIControlStateNormal gives an error and if i take it out the function doesnt work :( – Adedayo Sep 06 '14 at 00:36
  • @Ade dont need the state –  Sep 07 '14 at 10:57
  • I also have a UINavigationBar with some custom buttons and the default "back" button. How can I disable these too? self.navigationItem.backBarButtonItem.enabled = NO; does not work? – Adedayo Sep 19 '14 at 10:23
  • @Adedayo http://stackoverflow.com/questions/12123014/ios-disable-enable-back-button-from-detail-view –  Sep 19 '14 at 10:33
0

Disable all the scroll in the view

 - (void)disableScrollView
{
    for (UIScrollView *view in your_viewController.subviews) {

        if ([view isKindOfClass:[UIScrollView class]]) {

            view.scrollEnabled = NO;
        }
    }
}