如何让我的UISwitch做些什么?
问题描述:
如何让我的UISwitch调用此函数:https://ghostbin.com/paste/y2xrc打开时返回FALSE;当它关闭时。我得到了在Messages.app(MobileSMS.app)中显示“CKTranscriptCollectionViewController”内部的开关,现在我希望它在打开或关闭时执行某些操作,这是我在上面的链接中发布的内容)。如何让我的UISwitch做些什么?
#import <UIKit/UIKit.h>
#import <ChatKit/CKConversation.h>
@interface CKTranscriptCollectionViewController : UIViewController
@property (nonatomic, strong) UISwitch *mySwitch;
- (void)loadView;
@end
%hook CKTranscriptCollectionViewController
- (void)viewDidLoad {
%orig;
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(10, 10, 51, 31)];
[mySwitch setOn:NO animated:YES];
[self.view addSubview:mySwitch];
}
%end
答
[mySwitch addTarget:self action:@selector(switchValueChangeHandler:) forControlEvents:UIControlEventValueChanged];
答
在你的头文件中定义一个布尔值,
BOOL allowForceMMS;
-(BOOL)forceMMS {
return allowForceMMS;
)
- (void)viewDidLoad {
%orig;
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(10, 10, 51, 31)];
//add action on switch to handle switch on/off
[mySwitch addTarget:self action:@selector(setState:) forControlEvents:UIControlEventValueChanged];
[mySwitch setOn:NO animated:YES];
[self.view addSubview:mySwitch];
}
-(void)setState:(UISwitch*)switch {
allowForceMMS = switch.isOn;
}
答
在您的viewDidLoad
[switchTemp addTarget:self action:@selector(ChangeValue) forControlEvents:UIControlEventValueChanged];
做到这一点,这FUNC
-(BOOL)forceMMS
{
if (switchTemp.isOn)
{
return YES;
}
else
{
return NO;
}
}
答
你可以给目标要called..when开关改变..
- (void)viewDidLoad {
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(10, 10, 51, 31)];
[mySwitch addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
[mySwitch setOn:NO animated:YES];
[self.view addSubview:mySwitch];
}
-(void)valueChange:(UISwitch*)switch
{
if(switch.isOn)
{
//switch is on
}
else
{
//switch is off
}
}
希望它可以帮助你..!
答
只需创建在viewDidLoad方法如下一个UISwitch,
UISwitch *mySwitch = [[UISwitch alloc] init];
[mySwitch addTarget:self action:@selector(switchValueChangeHandler:) forControlEvents:UIControlEventValueChanged];
然后在开关变化的情况下的方法调用,检查状态 “ON”(布尔值是YES或n = NO)
-(void)switchValueChangeHandler:(UISwitch*)sender
{
NSLog(@"%@",sender.on);
}
然后根据sender.on是true或false,您可以执行您的操作。
@Hermang我忘了提及我正在使用theos – 2014-09-04 07:24:03