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.
Related
I have a very simple method, but it does not work. Ultimately, from input YES, I want to get NO and reverse.
-(void)myMethod:(BOOL)ys{
if (ys==YES) {
myLabel.hidden=NO;
myButton.hidden=NO;
}{
myLabel.hidden=YES;
myButton.hidden=YES;
}
}
Can you guys help me to correct and shorten the code? Thanks
Maybe this is a litle easier:
-(void)myMethod:(BOOL)ys{
myLabel.hidden = !ys;
myButton.hidden = !ys;
}
-(void)myMethod:(BOOL)visible
{
[myLabel setHidden:!visible];
[myButton setHidden:!visible];
}
The code should be working. However, your code is not working is probably because you did not set the referencing outlets for your label and button (If you created them with interface builder).
Referencing outlets should be set like this. Otherwise, the complier would not know if the button you want to hide is the button on the interface.
If you created the button/label with codes, check if you have them released before you are trying to set them visible or not please.
-(void)myMethod:(BOOL)ys{
if (ys) {
myLabel.hidden=NO;
myButton.hidden=NO;
} else {
myLabel.hidden=YES;
myButton.hidden=YES;
}
}
If we are trying to make it short...`
-(void)myMethod:(BOOL)ys{
myLabel.hidden = myButton.hidden = !ys;
}
I have this code:
while (!found) {
char letterGuess=(char)([[[wordList letterRank:lengthOfWord]objectAtIndex:yescount]intValue]+97);
NSString *stringLetter=[NSString stringWithFormat:#"%c",letterGuess];
if ([prevGuess count]<=0) {
alreadyGuessed=NO;
}else {
if ([prevGuess containsObject:stringLetter]) {
alreadyGuessed=YES;
yescount++;
}else{
alreadyGuessed=NO;
}
}
if (!alreadyGuessed) {
[prevGuess addObject:stringLetter];
[self drawYesNo];
NSLog(#"%c",letterGuess);
}
if (userAnswer) {
}else {
[self noGuess:letterGuess];
}
if ([wordList count]<=1) {
NSLog(#"word found");
found=YES;
}
}
basically, it takes a letter from a sorted array, checks it against another array containing all the previous letters that were entered, and if it is not in that array, it will call [self drawYesNo], which basically sets up and draws a yes and a no UIButton, and based on what the user presses, changes the 'userAnswer' variable.
My problem is that this loop executes so quickly that the objectAtIndex quickly exceeds the bounds of the array, and throws an error. I need some way to pause during the [drawYesNo] method, and allow the user to actually make a decision.
I know there are answers to similar questions on StackOverflow, but I just can't make heads or tails of them. Can someone please explain this for a very new OO programmer?
I can't really figure out all the logic of your code, but it sounds like you need to refactor it into different methods. Instead of having the code in a while loop, you should have one method that does most (all? I can't tell) of what you posted (without the while loop). You call this once when your app starts, then after the user clicks a button -- in the button's action method you change the value of userAnswer, and then call that method again. This keeps going until some condition is met in your button method that would cause it to stop.
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.
Ok so here's my code, it works great:
- (void)textViewDidChange:(UITextView *)textView{
if (textView==someObject) {
[detailItem setValue:textView.text forKey:#"someObjectAttribute"];
}
The problem is that I have lots of textviews instances to test for and I would prefer to find some other way to consolidate the code. I was thinking of something like a switch but I don't see how that would work. Any ideas?
One way would be to use the integer tag of each view. In your code, you’d have an enum like:
enum
{
kThingView,
kOtherView,
...
};
Each view's tag is set appropriately in IB or when setting up the view programatically. Then:
- (void) textViewDidChange:(UITextView *)textView
{
switch (textView.tag)
{
case kThingView:
...
}
}
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)