首页>>iPhone >>开发
cocos2d下加载UISwitch的例子
楼:1
九品仙
2011-9-15 18:27:23
cocos2d下加载UISwitch的例子
//cocos2d下加载UISwitch的例子.
UISwitch *bgmus_switch = [[[UISwitch alloc] initWithFrame:CGRectMake(120, 125, 50, 30)] autorelease];
[[[CCDirector sharedDirector] openGLView] addSubview: bgmus_switch];
[bgmus_switch addTarget:self action:@selector(adjust_vibration:) forControlEvents:UIControlEventValueChanged];
NSUserDefaults *userData = [NSUserDefaults standardUserDefaults];
  
NSString *vi = [userData objectForKey:vibration];
  
  
if([vi isEqualToString:@"1"]||vi==nil)
{
    [bgmus_switch setOn:YES];
}
else {
    [bgmus_switch setOn:NO];
}
 
//开关时将触发
-(void)adjust_vibration:(id)sender
{
    UISwitch *_switch =  (UISwitch*)sender;
    NSUserDefaults *userData = [NSUserDefaults standardUserDefaults];
    [userData setObject:[NSString stringWithFormat:@"%d",_switch.on] forKey:vibration];
}