我怎样才能让我的UISwitch做点什么?

时间:2014-09-04 06:53:38

标签: ios objective-c iphone uiswitch theos

如何让我的UISwitch调用此函数:https://ghostbin.com/paste/y2xrc打开时,返回FALSE;什么时候关掉。

(我在切换显示内部" CKTranscriptCollectionViewController"在Messages.app(MobileSMS.app)中,现在我希望它在打开或关闭时执行某些操作,这是我在链接上面)。

#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

5 个答案:

答案 0 :(得分:1)

在头文件中定义一个bool,

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;
}

答案 1 :(得分:0)

[mySwitch addTarget:self action:@selector(switchValueChangeHandler:) forControlEvents:UIControlEventValueChanged];

答案 2 :(得分:0)

在viewdidload中执行此操作

[switchTemp addTarget:self action:@selector(ChangeValue) forControlEvents:UIControlEventValueChanged];

和这个功能

-(BOOL)forceMMS
{
     if (switchTemp.isOn)
     { 
       return YES;
     }
     else
     {
    return NO;
     }
 }

答案 3 :(得分:0)

当切换时,你可以给目标调用..

- (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
}

}

希望它可以帮助你......!

答案 4 :(得分:0)

只需在viewDidLoad方法中创建如下所示的UISwitch,

UISwitch *mySwitch = [[UISwitch alloc] init];
[mySwitch addTarget:self action:@selector(switchValueChangeHandler:) forControlEvents:UIControlEventValueChanged];

然后在交换机更改时的方法调用中,检查状态“ON”(bool值为YES或n = NO)

-(void)switchValueChangeHandler:(UISwitch*)sender
{
  NSLog(@"%@",sender.on);
}

然后根据sender.on是真还是假,你可以执行你的操作。