2014-09-04 61 views
0

如何讓我的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 

回答

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

在你的頭文件中定義一個布爾值,

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

@Hermang我忘了提及我正在使用theos – 2014-09-04 07:24:03

0

在您的viewDidLoad

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

做到這一點,這FUNC

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

但我怎麼會這樣做來調用我在上面鏈接發佈的函數, - (BOOL)forceMMS? – 2014-09-04 07:27:12

+0

確實希望它被交換機或你自己調用 – Himanshu 2014-09-04 08:14:22

+0

@BrandonSalgado我把它改成你的funcs名字......當tap被點擊時,這個命令將被交換機調用,你可以通過[self forceMMS]手動調用它並保存BOOL值。 – Himanshu 2014-09-04 08:19:08

0

你可以給目標要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 
} 

} 

希望它可以幫助你..!

0

只需創建在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,您可以執行您的操作。