How to control the state of UISwitch programmatically and not by user in Objective-C?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a UiSwitch, which I want to disable it from being on and off by user. I want to be aware when user taps on it, and change its state programmatically if I want.
This code disables the switch but makes it faded. I don't want it because I want user tap on it.
[switch setEnabled:NO];
objective-c uiswitch
add a comment |
I have a UiSwitch, which I want to disable it from being on and off by user. I want to be aware when user taps on it, and change its state programmatically if I want.
This code disables the switch but makes it faded. I don't want it because I want user tap on it.
[switch setEnabled:NO];
objective-c uiswitch
add a comment |
I have a UiSwitch, which I want to disable it from being on and off by user. I want to be aware when user taps on it, and change its state programmatically if I want.
This code disables the switch but makes it faded. I don't want it because I want user tap on it.
[switch setEnabled:NO];
objective-c uiswitch
I have a UiSwitch, which I want to disable it from being on and off by user. I want to be aware when user taps on it, and change its state programmatically if I want.
This code disables the switch but makes it faded. I don't want it because I want user tap on it.
[switch setEnabled:NO];
objective-c uiswitch
objective-c uiswitch
edited Nov 24 '18 at 11:49
Fattaneh Talebi
asked Nov 24 '18 at 7:21
Fattaneh TalebiFattaneh Talebi
426825
426825
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
For whatever reason you might want to do that, one way to achieve it by adding UIView over the switch and add a tap recognizer to it to handle the tap, then you can set the switch on or off programatically. Consider the code below:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.switchControl = [[UISwitch alloc] initWithFrame:CGRectMake(10, 100, 0, 0 )];
[self.view addSubview:self.switchControl];
[self.switchControl setOn:YES animated:NO];
UIView *view = [[UIView alloc] initWithFrame:self.switchControl.frame];
[self.view addSubview:view];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapSwitch)];
[view addGestureRecognizer:tap];
}
- (void)didTapSwitch {
[self.switchControl setOn:NO animated:YES];
}
add a comment |
You can do something like this, the main idea is to find coordinates of the switch. If you have your switch in a view you can use hitTest:withEvent:
method instead
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UISwitch *mySwitch;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.mySwitch.userInteractionEnabled = NO;
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if (CGRectContainsPoint(self.mySwitch.frame, touchLocation)) {
[self.mySwitch setOn:!self.mySwitch.isOn];
}
}
@end
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456079%2fhow-to-control-the-state-of-uiswitch-programmatically-and-not-by-user-in-objecti%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
For whatever reason you might want to do that, one way to achieve it by adding UIView over the switch and add a tap recognizer to it to handle the tap, then you can set the switch on or off programatically. Consider the code below:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.switchControl = [[UISwitch alloc] initWithFrame:CGRectMake(10, 100, 0, 0 )];
[self.view addSubview:self.switchControl];
[self.switchControl setOn:YES animated:NO];
UIView *view = [[UIView alloc] initWithFrame:self.switchControl.frame];
[self.view addSubview:view];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapSwitch)];
[view addGestureRecognizer:tap];
}
- (void)didTapSwitch {
[self.switchControl setOn:NO animated:YES];
}
add a comment |
For whatever reason you might want to do that, one way to achieve it by adding UIView over the switch and add a tap recognizer to it to handle the tap, then you can set the switch on or off programatically. Consider the code below:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.switchControl = [[UISwitch alloc] initWithFrame:CGRectMake(10, 100, 0, 0 )];
[self.view addSubview:self.switchControl];
[self.switchControl setOn:YES animated:NO];
UIView *view = [[UIView alloc] initWithFrame:self.switchControl.frame];
[self.view addSubview:view];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapSwitch)];
[view addGestureRecognizer:tap];
}
- (void)didTapSwitch {
[self.switchControl setOn:NO animated:YES];
}
add a comment |
For whatever reason you might want to do that, one way to achieve it by adding UIView over the switch and add a tap recognizer to it to handle the tap, then you can set the switch on or off programatically. Consider the code below:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.switchControl = [[UISwitch alloc] initWithFrame:CGRectMake(10, 100, 0, 0 )];
[self.view addSubview:self.switchControl];
[self.switchControl setOn:YES animated:NO];
UIView *view = [[UIView alloc] initWithFrame:self.switchControl.frame];
[self.view addSubview:view];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapSwitch)];
[view addGestureRecognizer:tap];
}
- (void)didTapSwitch {
[self.switchControl setOn:NO animated:YES];
}
For whatever reason you might want to do that, one way to achieve it by adding UIView over the switch and add a tap recognizer to it to handle the tap, then you can set the switch on or off programatically. Consider the code below:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.switchControl = [[UISwitch alloc] initWithFrame:CGRectMake(10, 100, 0, 0 )];
[self.view addSubview:self.switchControl];
[self.switchControl setOn:YES animated:NO];
UIView *view = [[UIView alloc] initWithFrame:self.switchControl.frame];
[self.view addSubview:view];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapSwitch)];
[view addGestureRecognizer:tap];
}
- (void)didTapSwitch {
[self.switchControl setOn:NO animated:YES];
}
answered Nov 24 '18 at 11:52
gulyashkigulyashki
36125
36125
add a comment |
add a comment |
You can do something like this, the main idea is to find coordinates of the switch. If you have your switch in a view you can use hitTest:withEvent:
method instead
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UISwitch *mySwitch;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.mySwitch.userInteractionEnabled = NO;
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if (CGRectContainsPoint(self.mySwitch.frame, touchLocation)) {
[self.mySwitch setOn:!self.mySwitch.isOn];
}
}
@end
add a comment |
You can do something like this, the main idea is to find coordinates of the switch. If you have your switch in a view you can use hitTest:withEvent:
method instead
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UISwitch *mySwitch;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.mySwitch.userInteractionEnabled = NO;
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if (CGRectContainsPoint(self.mySwitch.frame, touchLocation)) {
[self.mySwitch setOn:!self.mySwitch.isOn];
}
}
@end
add a comment |
You can do something like this, the main idea is to find coordinates of the switch. If you have your switch in a view you can use hitTest:withEvent:
method instead
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UISwitch *mySwitch;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.mySwitch.userInteractionEnabled = NO;
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if (CGRectContainsPoint(self.mySwitch.frame, touchLocation)) {
[self.mySwitch setOn:!self.mySwitch.isOn];
}
}
@end
You can do something like this, the main idea is to find coordinates of the switch. If you have your switch in a view you can use hitTest:withEvent:
method instead
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UISwitch *mySwitch;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.mySwitch.userInteractionEnabled = NO;
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if (CGRectContainsPoint(self.mySwitch.frame, touchLocation)) {
[self.mySwitch setOn:!self.mySwitch.isOn];
}
}
@end
answered Nov 24 '18 at 11:52
schmidt9schmidt9
2,4311724
2,4311724
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456079%2fhow-to-control-the-state-of-uiswitch-programmatically-and-not-by-user-in-objecti%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown