uiswitch
How to create a UITableViewCell with a UISwitch and get the data?
You could just add the UISwitch in your accessory view. That is the easiest way to do it, not to mention that it looks ‘elegant’. Then, in your tableview controller code, you could just call a selector every time the switch is toggled, or even toggle the switch itself by getting the switch’s current status … Read more
Changing UISwitch width and height
I tested the theory and it appears that you can use a scale transform to increase the size of the UISwitch UISwitch *aSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(120, 120, 51, 31)]; aSwitch.transform = CGAffineTransformMakeScale(2.0, 2.0); [self.view addSubview:aSwitch];
How to know the UITableview row number
Tags, subclasses, or view hierarchy navigation are too much work!. Do this in your action method: CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView]; NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint]; Works with any type of view, multi section tables, whatever you can throw at it – as long as the origin of your sender is within the cell’s … Read more
Swift/UISwitch: how to implement a delegate/listener
UISwitch has no delegate protocol. You can listen to the status as follows: ObjC: // somewhere in your setup: [self.mySwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged]; – (void)switchChanged:(UISwitch *)sender { // Do something BOOL value = sender.on; } Swift: mySwitch.addTarget(self, action: “switchChanged:”, forControlEvents: UIControlEvents.ValueChanged) func switchChanged(mySwitch: UISwitch) { let value = mySwitch.on // Do something } Swift3 : … Read more
Swift – How to set initial value for NSUserDefaults
Swift 3 syntax example Register a boolean default value: UserDefaults.standard.register(defaults: [“SoundActive” : true]) And to get the value: UserDefaults.standard.bool(forKey: “SoundActive”) Sidenote: Although the above code will return true, note that the value isn’t actually written to disk until you set it: UserDefaults.standard.set(true, forKey: “SoundActive”)
iPhone: Save boolean into Core Data
Core Data “does not have” a Boolean type (it does, but it is an NSNumber). So to set the equivalent of useGPS = YES. [entity setUseGPS:[NSNumber numberWithBool:YES]]; And the other way around: BOOL isGPSOn = [[entity useGPS] boolValue]; Update: As pointed out by SKG, With literals in Objetive-C you can now do it in a … Read more
How to call an action when UISwitch changes state?
[yourSwitchObject addTarget:self action:@selector(setState:) forControlEvents:UIControlEventValueChanged]; This will call the below method when your switch state changes – (void)setState:(id)sender { BOOL state = [sender isOn]; NSString *rez = state == YES ? @”YES” : @”NO”; NSLog(rez); }
iPhone UIButton with UISwitch functionality
UIButton already supports a “switch” functionality. Just set a different image in Interface Builder for the “Selected State Configuration”, and use the selected property of UIButton to toggle its state.
UISwitch in a UITableView cell
Setting it as the accessoryView is usually the way to go. You can set it up in tableView:cellForRowAtIndexPath: You may want to use target/action to do something when the switch is flipped. Like so: – (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { switch( [indexPath row] ) { case MY_SWITCH_CELL: { UITableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier:@”SwitchCell”]; if( … Read more