How do I set a switch to turn off a switch? - ios

I am trying to set a switch to turn off another switch when it is on. I know you use a if statement to do this, but I find it to be very different than an HTML if statement. I know my code is not right but it will explain what I need.
- (IBAction)dashieScheme:(id)sender {
if(dashieScheme.state = true) {
(twilightScheme.state = false);
}
}
- (IBAction)twilightScheme:(id)sender {
if(twilightScheme:(id)sender ) {
(dashieScheme.state = false);
}
}

You want to use the following:
[dashieScheme setOn:NO animated:YES];
Also, your if statement needs some work. You want to use == to check for equality. a single = sign is for assignment.

Related

how to identify UITextField in a better way

I am using xcode8+swift3.
I have multiple UITextField in my controller view. Each UITextField has a outlet connection in code.
I know I can use “tag” to identify UITextField, but it seems I can only use number as tag (I tried with string value for tag field, my Xcode always get stuck, only number as tag works).
But I don’t want to use magic number in my code like:
If (textField.tag == 0) {
}
I am wondering, is there a better way or more descriptive way in code to identify UITextField?
Tag is the correct tool. Just create an enum for them to track.
enum FieldIdentifier: Int {
case name = 0
case age = 1
}
if let fieldIdentifier = FieldIdentifier(rawValue: textField.tag) {
switch fieldIdentifier {
case .name: ...
case .age: ...
}
}
(Note that Larme's comment about using == is also appropriate, and if you already have outlets is better.)

Why is (self.window) in the following code a condition?

Why is (self.window) in the following code a condition?
-(void)beginRefreshing {
[UIView animateWithDuration:MJRefreshFastAnimationDuration
animations:^{
self.alpha = 1.0;
}];
self.pullingPercent = 1.0;
if (self.window) {
self.state = MJRefreshStateRefreshing;
} else {
self.state = MJRefreshStateWillRefresh;
[self setNeedsDisplay];
}
}
In this case:
if (self.window) {
which is a short version of
if (self.window != nil) {
is a test whether the view (UIView instance) is in the view hierarchy of any window, that is, whether the view is displayed on the screen.
Certain variables are boolean meaning they can either be equal to true or false. Others can have a wide range of values. for example off you want to do something if an Integer is equal to 5 you can write:
if (myInt == 5) {
//do something
}
If your variable is a Boolean there are only two options so we can write is shorter:
if (myBoolean) {
//case where myBoolean = true
} else {
//case where myBoolean = false
}
In your particular example, self.window evaluates to a Boolean in the following manner: either self.windows exists in the view hierarchy or not (self.windows == true) or it do not (self.windows == false).
I've decided to expand my comment to an answer for future reference.
It's actually a side effect, AFAIK. Because false is defined as 0, and true is defined as anything different than 0. A valid objects address is non-zero so it is treated as true, whereas, thanks to ARC, nil == 0. You can check this by printing this NSLog(#"%p", nil). Bear in mind, that for integers, this means that a negative value, also evaluates to true
It is also worth noting, that because the underlying storage for a boolean is rarely one-bit it may not always be true that when someBool == true, then someBool == 1. This means that taking such a shortcut :
- (NSInteger)increment:(NSInteger)toIncrement ifTrue:(BOOL)condition {
return toIncrement + condition;
}
may not always lead to toIncrement being incremented by 1. The code is valid, becasue a BOOL will get promoted to integer automatically.
This is of course somwhat of an edge case, but still possible.

UISwitch won't let me set default state

I have a custom UICell that contains a UISwitch element. When I render my table, cellForRowAtIndexPath() will check to see if the users preference matches a specific category, if it does they are subscribed, else they are not. I have written some logic to toggle the switch based on this result:
if let userPreferences = user["preferences"] as? Array<AnyObject>
{
if userPreferences[indexPath.row].objectId == game.getGameId() {
prefCell.subscribed?.setOn(false, animated: true)
}
}
I have breakpointed the line where the switch is disabled, and it breaks 9 times out of the current 11 items I retrieve in the userPreferences collection, however none of the switches on my UI are updated. Why is this happening?
EDIT: After some further testing it appears that the if statement is always satisfied, this is extremely strange as in the event that the strings don't match, Swift still claims that they did.
For example if I have a list of the ID's
xyz12345
abc12345
efg12345
And the following loop executed:
if let userPreferences = user["preferences"] as? Array<AnyObject>
{
for preference in userPreferences
{
if preference.objectId == game.getGameId()
{
prefCell.subscribed.on = true;
} else {
prefCell.subscribed.on = false;
}
}
}
The first comparison could be something like preference = xyz12345 and game.getGameId() = abc12345. I'd expect the second case to be invoked as these strings certainly do not match, however the case passes as true and the switch is set, why am I witnessing this behaviour?

Type True/False in Umbraco

We want to implement a check box[Type : true/false] in an DocumemtType in Umbraco.
Our current Project necessity is:
an check box which will decide whether an image should be an link or popup
The code goes this way ...
var child= #Model;
if(child.GetProperty("popUp").Value.ToString() == "1")
{
// true means image will act as popup
}
else
{
// false means image will act as link
}
But the problem is an error is occurred "Cannot perform runtime binding on a null reference"
I have also tried code like ,
if (child.GetProperty("popup").Value.Equals("1"))
{
}
or
if (child.GetProperty("popup").Value.ToString().Equals("1"))
{
}
but still not able to get it. All suggestions are welcomed .
node.GetProperty("popUp") is the way to go. If your control value is actually string, then your check logic would look like
if (node.GetProperty<string>("popUp") == "1"){}
Effectively generic GetProperty is what your code does, but it handles the null case, returning default(string).
(I have never used the dynamic thing, in case something will go wrong there, do the typed var node = new Node(id);)
Since you recently added the property to the document type, unless each node of that type has been published, the property will return null. You'll need to check if the property is null first then check if its true.
var popUp = child.GetProperty("popUp");
if (popUp != null && popUp.Value.Equals("1"))
{
// popup...
}
else
{
// link...
}
Used the below code and it worked fine for me
var child= #Model;
if(#child.popUp)
{
// true means image will act as popup
}
else
{
// false means image will act as link
}
Use this:
var child= #Model;
if(child.GetPropertyValue<bool>("popUp", false))
{
// true means image will act as popup
}
else
{
// false means image will act as link
}

iOS Compare Value of Label

I'm using a label to display the string result of function. However I have a class variable that stores the previous result and I need to update that variable in different ways depending on different conditions. The code I wrote is
if(displayPassword.text == #"Memorable")
{
prevpass = [newPassword returnPassword];
}
else
{
prevpass = displayPassword.text;
}
However it always jumps to the else as it seems to show under debugging that displayPassword.text is always empty depsite it showing a value.
You can only use == to compare scalar values. A string is an object. You need to use the isEqual: or isEqualToString: method instead.
if([displayPassword.text isEqualToString:#"Memorable"]) {

Resources