I'm new to Cocoa, and working my way through Hillegass's book. I'm having trouble with chapter 20's challenge 2.
The challenge is to add checkbox's that toggle if the letter being drawn is italic, bold, or both.
-(IBAction)toggleItalic:(id)sender
{
int state = [italicBox state];
NSLog(#"state %d", state);
if (state = 1) {
italic = YES;
NSLog(#"italic is yes");
}
else {
italic = NO;
NSLog(#"italic is no");
}
}
Right now, this snippet of code is returning yes when the box is checked, and when the box is unchecked. What am I doing wrong?
Thanks,
Justin.
Your problem lies in your if statement:
if (state = 1) {
you are assigning state to the value 1: state = 1, while what you need to test is if state is currently 1: state ==1
This is a fairly common mistake (especially in languages that allow assignment in if statements). One trick to learn to get around this is to make your comparison checks like so:
if (1 == state)
You cannot assign 1 to another value. Therefore, if you mistakenly use = instead of == you will get a compiler error and it's an easy fix.
Use comparison instead of assignment and use proper enums instead of hardcoded values that could change:
if (state == NSOnState)
else if (state == NSOffState)
Related
Does anyone know if it is possible to omit / hide a column from the filter (list of checkboxes) on KoGrid? If so, how? (I'm hoping there's something that can be done to achieve this in the ColumnDefs property)
(Answering own question, in case it helps others). What I ended up doing, is subscribing to the Grid's showMenu() observable, and hiding elements that pertained to columns with labels that were empty string or only whitespace.
self.Grid().showMenu.subscribe(function (val) {
if (val != true) return;
var colDefId = 0;
self.gridOptions.columnDefs.forEach(function (colDef) {
if (!colDef) return;
if (!/\S/.test(colDef.displayName)) $($('.kgColListItem')[colDefId]).hide();
colDefId++;
});
});
For some reason my boolean seems to be returning true, even when the else should be getting called during testing:
if([photoId isEqualToString:object]) {
liked = TRUE;
cell.likedButton.imageView.image = [UIImage imageNamed:#"likedBtn.png"];
break;
} else {
liked = FALSE;
cell.likedButton.imageView.image = [UIImage imageNamed:#"likeBtn.png"];
}
To clarify a little more, photoId is sometimes equal to the object. But in one case, it isn't equal to it. The images in my cell are changing to the right files contingent on the equal method however, the boolean seems to only return yes.
Any ideas why this is happening? Does it have something to do with my break?
You should only put a break statement when you want to terminate a loop. Perhaps you're running this conditional inside a loop so once the statement is TRUE, your entire loop stops (break).
Since it is in a loop it is breaking after the first time it is true. You should put the break in the else statement if you want it to break if it is false.
I'm writing a part of an application for a movie ticket program.
Basically, I have to write an If statement verifying the age put in a textbox, based on a radiobox that is checked.
So, if the radiobox "PG" is checked, the age in the textbox has to be equal to or greater than 12. If "Restricted" is checked, then the textbox has to be equal to or great than 17.
Can anyone help me out with this? I'd appreciate it a lot.
Thank you!
I'm working on the assumption that this is an asp.net app?
If that's the case, the best approach is to make the radio button and the text postback by setting its Autopostback property to true. On server-side, check the combination and act accordingly.
Use a Switch statement to check the age based on the rating selected.
You could wrap these in an UpdatePanel to prevent visible post-back.
psuedo code:
rating_changed() {
checkAge();
}
txtAge_changed() {
checkAge();
}
void checkAge() {
bool ageOkay = false;
int age = Convert.ToInt32(txtAge.Text);
switch (rating.SelectedItem.Value) {
case "G":
ageOkay = true;
break;
case "PG":
if (age >= 8) ageOkay = true;
break;
case "PG-13":
if (age >= 13) ageOkay = true;
break;
}
if (ageOkay) {
//do next task
} else {
//you're not old enough
}
}
I have a SharePointWebControls:UserField in a page layout that needs to be excluded from spell checking, as otherwise whenever a user is selected there are a large number of spelling errors are detected in the code-behind for the control.
It seems that in Sharepoint 2007 this behaviour could be implemented by using excludefromspellcheck = "true" but this doesn't seem to work for Sharepoint 2010. Has anyone come across the same problem and found a way around it?
Based on SpellCheckEntirePage.js, that appears to still be the way:
var elements=document.body.getElementsByTagName("*");
for (index=0; index < elements.length;++index)
{
if (null !=elements[index].getAttribute("excludeFromSpellCheck"))
{
continue;
}
// snipped - if (elements[index].tagName=="INPUT")
// snipped - else if (elements[index].tagName=="TEXTAREA")
}
But excludeFromSpellCheck is not a property of UserField, so it probably won't automatically copy down to the rendered HTML. When rendered, the UserField control is made up of several elements. I would try looking at the View Source to see if excludeFromSpellCheck is making it into the final HTML. But to set the attribute on the appropriate elements, you might need to use some jQuery like this:
$("(input|textarea)[id*='UserField']").attr("excludeFromSpellCheck", "true");
You can disable the spell check for certain fields by setting the "excludeContentFromSpellCheck" attribute to "true" on text area and input controls that you dont want to be spell checked.
I did this on all my page layouts. Now i dont get false positives anymore.
The solution is to add a div tag around the fields you don't want spell checked and adding a javascript that sets "excludeFromSpellCheck" to "true" for the elements within the div tag.
The solution i found is described here: Inaccurate Spell Check on SharePoint Publishing Pages
Joe Furner posted this solution, which has worked for me.
https://www.altamiracorp.com/blog/employee-posts/spell-checking-your-custom-lay
It excludes all PeoplePickers on the page:
function disableSpellCheckOnPeoplePickers() {
var elements = document.body.getElementsByTagName("*");
for (index = 0; index < elements.length; index++) {
if (elements[index].tagName == "INPUT" && elements[index].parentNode && elements[index].parentNode.tagName == "SPAN") {
var elem = elements[index];
if (elem.parentNode.getAttribute("NoMatchesText") != "") {
disableSpellCheckOnPeoplePickersAllChildren(elem.parentNode);
}
}
}
}
function disableSpellCheckOnPeoplePickersAllChildren(elem) {
try {
elem.setAttribute("excludeFromSpellCheck", "true");
for (var i = 0; i < elem.childNodes.length; i++) {
disableSpellCheckOnPeoplePickersAllChildren(elem.childNodes[i]);
}
}
catch(e) {
}
}
This code is working partially only,because if you put the people picker value again checking the people picker garbage value for one time.
What is the best way when setting up a UI for the iPhone to differentiate between multiple items of the same type (i.e. 2 sliders) currently I am assigning the controls unique "tags" and then querying them in Xcode. Is this the way to go or am I missing something else?
-(IBAction)switchChanged:(UISwitch*)senderSwitch {
if([senderSwitch tag] == 1) {
NSLog(#"SwitchChanged: Engines");
}
...
gary
Hey Gary! I usually choose similar approach, though I prefer using integer constants instead of 'raw' numbers. Here's an example:
const int VIEW_TAG_A_SWITCH = 1;
const int VIEW_TAG_OTHER_SWITCH = 2;
- (IBAction)switchChanged:(UISwitch*)senderSwitch {
switch (senderSwitch.tag) {
case VIEW_TAG_A_SWITCH:
// do something
break;
case VIEW_TAG_OTHER_SWITCH:
// do something else
break;
}
}
This makes code more informational and help you to deal with situations when you need to change the UIView tag value for some reason (this way you only change it once in your nib and once in your code).
It's also very handy to use enums when you need to work with a group of controls. Here's a dummy single selection group of buttons example (something similar to what <input type="option" />):
enum GenderOptionButtons {
kMaleGenderOptionButton = 10,
kFemaleGenderOptionButton,
kUndefinedGenderOptionButton,
NUM_GENDER_OPTION_BUTTONS
}
- (IBAction)buttonDidTouchUpInside:(UIButton *)senderButton {
for (int i = kMaleGenderOptionButton; i < NUM_GENDER_OPTION_BUTTONS; i ++) {
[(UIButton *)[self.view viewWithTag:i] setSelected:NO];
}
senderButton.selected = YES;
}
If you don't own direct reference (i.e. IBOutlet) to controls, then the "tag" approach is ok.
One advantage of this approach is that you can have different kind of controls calling the same action method. One major drawback of this approach is you have to keep in sync the tags between IB and XCode.