checking user input via textfield - cocoa

I've been struggling with this for quite a few days now; my app has a diagram with uitextfields to represent labelling of the picture. I would like to check the user input against a dictionary (for the answer) and if it is correct, increase the score by 1.
I had it working by 'hard coding' each of the textfield.text queries each with their own if statement, but I would like a better and more reusable way if possible?
I've tried this so far:
- (IBAction)checkAnswers:(UITextField *)textField
{
// array for each textfield
allTextfields = [[NSArray alloc] initWithObjects:eyepiece, objectiveLenses, focussingKnobs, stage, mirror, nil];
// array for each UIImageView
allTicks = [[NSArray alloc] initWithObjects:eyepieceTick, objectiveTick, focussingTick, stageTick, mirrorTick, nil];
UIImage *image = [UIImage imageNamed:#"Tick.png"];
for (textField in allTextfields) {
if ([textField.text isEqualToString:[[microscopeBrain.microscopeDictionary valueForKey:theTextfieldTag] valueForKey:#"Answer"]]) {
[[allTicks objectAtIndex:textField.tag] setImage:image];
x++;
textField.enabled = NO;
NSLog(#"%#", microscopeBrain.microscopeDictionary);
// NSLog(#"%#", [[microscopeBrain.microscopeDictionary valueForKey:theTextfieldTag] valueForKey:#"Answer"]);
}
finalMicroscopeScore = [[NSString alloc] initWithFormat:#"%i", x];
microscopeScoreLabel.text = [[NSString alloc] initWithFormat:#"%i", x];
}
}
The problem is that even if the answers are in the wrong textfield, as long as one is correct, they will all show up as right, which is kind of embarrassing!
Any help would be very much appreciated.

Try changing the valueForKey:theTextFieldTag to valueForKey:textField.tag and see if that helps. You don't show how you get the value for theTextFieldTag, so I'm not sure if that's the problem.

Related

Download Images, Display Them in UIImageView, and SORT

Ok guys, I am fairly new to objective C. I have an app that downloads a list of images from my web server and displays the appropriate ones in a UIImage view with swipe gestures to go forward or backwards for the next/previous pic to be displayed. Current naming format for the pictures is like this:
uploaded_141_admin1.png
uploaded_141_admin2.png
uploaded_141_interior1.png
uploaded_141_interior2.png
uploaded_141_exterior1.png
The current code loads every picture into the view that has 141 in the middle part of the filename (or whatever record the user in on... 141 is variable in this instance, just showing here for an example of the format). The problem is, there seems to be no rhyme or reason as to what order they are displayed in. I would like it to use the last part of the filename to sort alphabetically (or even the whole filename, as it would achieve the same result). In the example above, it would display the downloaded pics in the following order when swiping through the uiimageiew:
uploaded_141_admin1.png
uploaded_141_admin2.png
uploaded_141_exterior1.png
uploaded_141_interior1.png
uploaded_141_interior2.png
I've search and can't find what I am looking for (maybe because I'm using the wrong search criteria). Here is my existing code that downloads and displays the images in the UIImageView. I assume the "sort" code would go in here somewhere:
-(void)downloadPictures:(NSArray *)picPaths {
ELog(#"Downloading pictures: %#",picPaths);
// wait indicator
[[WaitingView sharedInstance] setMessage:LocStr(#"Loading pictures... The more pictures there are, the longer this will take. Please be patient.")];
[[WaitingView sharedInstance] showIndicator:YES];
[[WaitingView sharedInstance] displayOn:[self view]];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
// queue download operation
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSInvocationOperation *downloadOp = [[NSInvocationOperation alloc] initWithTarget:self selector:#selector(downloadOperation:) object:picPaths];
[queue addOperation:downloadOp];
}
-(void)downloadOperation:(NSArray *)picPaths {
NSMutableArray *allPictures = [[NSMutableArray alloc] init];
for(NSString *path in picPaths) {
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://%#%#/%#/%#",SERVER_ADDRESS,SERVER_PORT,SERVER_PHOTOS,path]];
NSData *picData = [NSData dataWithContentsOfURL:url];
if(picData!=nil) {
UIImage *img = [UIImage imageWithData:picData];
if(img!=nil) {
[allPictures addObject:img];
} else {
ELog(#"Failed to convert data to image from url %#",url);
}
} else {
ELog(#"Failed to download image from url %#",url);
}
}
[[WaitingView sharedInstance] performSelectorOnMainThread:#selector(remove) withObject:nil waitUntilDone:NO];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
self.pictures=allPictures;
if([self.pictures count]==0) {
[self performSelectorOnMainThread:#selector(downloadErrorMessage) withObject:nil waitUntilDone:NO];
} else {
self.currentIndex=0;
[self performSelectorOnMainThread:#selector(showPicture) withObject:nil waitUntilDone:NO];
}
}
-(void)downloadErrorMessage {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Oooops!" message:LocStr(#"Pictures download failed") delegate:nil cancelButtonTitle:LocStr(#"Close") otherButtonTitles:nil];
[alert show];
[alert release];
[self goBack];
}
-(void)showPicture {
UIImage *image = [self.pictures objectAtIndex:self.currentIndex];
ELog(#"Now displaying image with index %d: %#",self.currentIndex,image);
self.picture.image=image;
[self.picture setNeedsLayout];
}
In your downloadPictures: method you should sort your picPaths array to be the order you want the images before you start the download operation. You can do this by creating a new sorted array using the NSArray method sortedArrayUsingSelector:. Using caseInsensitiveCompare: as the selector for the sort will order the NSStrings in the array alphabetically.
NSArray *sortedPicPaths = [picPaths sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
Then when you init your NSInvocationOperation, pass the sorted array as the object.

contacts data in xcode

Everybody..
I am trying to develop an app for iPhone which basicly deals with ABAddressBook and contacts data...
My goal is to send all the contacts data (at first step, names and phones) in a file by e-mail..
Now, I am trying to reach the contacts data, and I want to add them to two different arrays, names and phones arrays..
At first, I am trying to see all the datas in the screen when i pressed a button "List Contacts". the datas should be seen on the screen. and then when i pressed the second button "Send Contacts", it should send the file to an e-mail account.. This is how my apps will work..
I am having problems at showing the data on the screen.. I wrote something but it doesn't give anything on the screen in a textView..
Can you help me to solve this problem?
Here's the code for listing the contacts (listCon method):
-(IBAction)listCon:(id)sender
{
NSMutableArray *names = [[NSMutableArray alloc] init];
NSMutableArray *numbers1= [[NSMutableArray array] init];
NSMutableArray *numbers2= [[NSMutableArray array] init];
NSMutableArray *numbers3= [[NSMutableArray array] init];
ABAddressBookRef addressBook = ABAddressBookCreate();
if (addressBook != nil)
{
NSLog(#"Successfully accessed the address book.");
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople= ABAddressBookGetPersonCount(addressBook);
NSUInteger peopleCounter = 0;
for (peopleCounter = 0;peopleCounter < nPeople; peopleCounter++)
{
ABRecordRef thisPerson = CFArrayGetValueAtIndex(allPeople,peopleCounter);
NSString *contactFirstLast = [NSString stringWithFormat:#"%,%",ABRecordCopyValue(thisPerson, kABPersonFirstNameProperty), ABRecordCopyValue(thisPerson,kABPersonLastNameProperty)];
[names insertObject:contactFirstLast atIndex:peopleCounter];
ABMultiValueRef phoneNumbers = ABRecordCopyValue(thisPerson,kABPersonPhoneProperty);
NSString *firstPhone = (__bridge_transfer NSString*) ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
NSString *secondPhone = (__bridge_transfer NSString*) ABMultiValueCopyValueAtIndex(phoneNumbers, 1);
NSString *thirdPhone = (__bridge_transfer NSString*) ABMultiValueCopyValueAtIndex(phoneNumbers, 2);
[numbers1 insertObject:firstPhone atIndex:peopleCounter];
[numbers2 insertObject:secondPhone atIndex:peopleCounter];
[numbers3 insertObject:thirdPhone atIndex:peopleCounter];
}
}
myView.text=[names componentsJoinedByString:#"\n\n"];
myView.text=[numbers1 componentsJoinedByString:#"\n\n"];
myView.text=[numbers2 componentsJoinedByString:#"\n\n"];
myView.text=[numbers3 componentsJoinedByString:#"\n\n"];
}
Just glancing at your code, you can't do this:
NSString *contactFirstLast = [NSString stringWithFormat:#"%,%",ABRecordCopyValue(thisPerson, kABPersonFirstNameProperty), ABRecordCopyValue(thisPerson,kABPersonLastNameProperty)];
There are several errors: first off % in your stringWithFormat: is not a format specifier; you probably are thinking of %#. Second off, copying the value of kABPersonFirstNameProperty will return a CFStringRef, and that's not what you want to display the name in a text field. You'll have to toll-free bridge the result of ABRecordCopyValue(). You can do this by adding this line - (__bridge_transfer NSString *) - in front of your ABRecordCopyValue()'s. With all the corrections, it should look like this:
NSString *contactFirstLast = [NSString stringWithFormat:#"%#,%#", (__bridge_transfer NSString *)ABRecordCopyValue(thisPerson, kABPersonFirstNameProperty), (__bridge_transfer NSString *)ABRecordCopyValue(thisPerson,kABPersonLastNameProperty)];
Hope this help (might not cover all errors)!

Change a loaded image based on a NSUserDefaults setting

Hope you can help me with this.
I have an app that needs to show one of two maps via the setting of a UISwitch. The settings.bundle is all set up and I am trying to write an If statement to determine if the switch is on or off, and to display the right image.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL Enabled = [defaults boolForKey:#"zones_preference"];
if (Enabled == #"Enabled") {
[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"withzones.jpg"]];
}
else {
[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"withoutzones.jpg"]];
}
This builds without error, but doesn't load the image into the ScrollView. Could anyone advise on where I am going wrong?
Well, the code you've posted only creates a UIImageView object and doesn't do anything more. It's a leak too.
There's one more error in the line
if (Enabled == #"Enabled") {
Here, you are comparing a boolean to a string which will evaluate to false automatically so it needs to be corrected too.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL enabled = [defaults boolForKey:#"zones_preference"];
UIImageView * imageView;
if ( enabled ) {
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"withzones.jpg"]];
} else {
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"withoutzones.jpg"]];
}
imageView.frame = imageViewFrame; // Where "imageViewFrame" is an appropriate frame.
[scrollView addSubview:imageView];
[imageView release];
Deepak, that was very useful and thank you. I had been working with a variable, but the mistake I had made was that I was trying to add the setup of the variable in with the creation of the UIImageView.
I'll work with this and see how I get on.

How to optimally fit a NSString in a rectangle? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Change NSTextField font size to fit
I am trying to fit a string of variable length (the number of words in the string is unknown) inside a given rectangle. I want to optimally size the string so that it is as big as possible and fits inside the rectangle. Further more, the string should word wrap if there is more than one word and that a word should not be partially rendered on multiple lines. My problem is sometimes a word is partially laid out on multiple lines as seen below. Any suggestions on what I might be doing wrong?
Thank you.
I am using an NSLayoutManager, NSTextStorage and NSTextContainer.
I initialize everything as follows:
textStorage = [[NSTextStorage alloc] initWithString:#""];
layoutManager = [[NSLayoutManager alloc] init];
textContainer = [[NSTextContainer alloc] init];
[layoutManager addTextContainer:textContainer];
[textStorage addLayoutManager:layoutManager];
paraStyle = [[NSMutableParagraphStyle alloc] init];
[paraStyle setLineBreakMode:NSLineBreakByWordWrapping];
[paraStyle setParagraphStyle:[NSParagraphStyle defaultParagraphStyle]];
[paraStyle setAlignment:NSCenterTextAlignment];
I then compute the font size as follows,
- (float)calculateFontSizeForString:(NSString *)aString andBoxSize:(NSSize)aBox
{
//Create the attributed string
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:aString];
[textStorage setAttributedString:attrString];
[textContainer setContainerSize:NSMakeSize(aBox.width, FLT_MAX)];
[attrString release]; //Clean up
//Initial values
float fontSize = 50.0;
float fontStepSize = 100.0;
NSRect stringRect;
BOOL didFindHeight = NO;
BOOL shouldIncreaseHeight = YES;
while (!didFindHeight)
{
NSMutableDictionary *stringAttributes = [NSMutableDictionary dictionaryWithObjectsAndKeys:
paraStyle, NSParagraphStyleAttributeName,
[NSFont systemFontOfSize:fontSize], NSFontAttributeName, nil];
[textStorage addAttributes:stringAttributes range:NSMakeRange(0, [textStorage length])];
(void)[layoutManager glyphRangeForTextContainer:textContainer];
stringRect = [layoutManager usedRectForTextContainer:textContainer];
if (shouldIncreaseHeight)
{
if (stringRect.size.height > aBox.height)
{
shouldIncreaseHeight = NO;
fontStepSize = fontStepSize/2;
}
fontSize += fontStepSize;
}
else
{
if (stringRect.size.height < aBox.height)
{
shouldIncreaseHeight = YES;
fontStepSize = fontStepSize/2;
if (fontStepSize <= 0.5)
{
didFindHeight = YES;
}
}
if ((fontSize - fontStepSize) <= 0)
{
fontStepSize = fontStepSize/2;
}
else
{
fontSize -= fontStepSize;
}
}
}
return fontSize;
}
Please search before posting. This comes up repeatedly. Latest answer is here, but I think there're more complete answers with code listings elsewhere.
My admittedly simple example shows how to do it without a text container and layout manager but your approach is more robust. Unfortunately brute-force (sizing down until it fits) is the only approach for determining the best fit.

Changing NSApplicationIcon across a running application?

I'd like to adjust the NSApplicationIcon image that gets shown automatically in all alerts to be something different than what is in the app bundle.
I know that it's possible to set the dock icon with [NSApplication setApplicationIconImage:] -- but this only affects the dock, and nothing else.
I'm able to work around this issue some of the time: I have an NSAlert *, I can call setIcon: to display my alternate image.
Unfortunately, I have a lot of nibs that have NSImageView's with NSApplicationIcon, that I would like to affect, and it would be a hassle to create outlets and put in code to change the icon. And for any alerts that I'm bringing up with the BeginAlert... type calls (which don't give an NSAlert object to muck with), I'm completely out of luck.
Can anybody think of a reasonable way to globally (for the life of a running application) override the NSApplicationIcon that is used by AppKit, with my own image, so that I can get 100% of the alerts replaced (and make my code simpler)?
Swizzle the [NSImage imageNamed:] method? This method works at least on Snow Leopard, YMMV.
In an NSImage category:
#implementation NSImage (Magic)
+ (void)load {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// have to call imageNamed: once prior to swizzling to avoid infinite loop
[[NSApplication sharedApplication] applicationIconImage];
// swizzle!
NSError *error = nil;
if (![NSImage jr_swizzleClassMethod:#selector(imageNamed:) withClassMethod:#selector(_sensible_imageNamed:) error:&error])
NSLog(#"couldn't swizzle imageNamed: application icons will not update: %#", error);
[pool release];
}
+ (id)_sensible_imageNamed:(NSString *)name {
if ([name isEqualToString:#"NSApplicationIcon"])
return [[NSApplication sharedApplication] applicationIconImage];
return [self _sensible_imageNamed:name];
}
#end
With this hacked up (untested, just wrote it) jr_swizzleClassMethod:... implementation:
+ (BOOL)jr_swizzleClassMethod:(SEL)origSel_ withClassMethod:(SEL)altSel_ error:(NSError**)error_ {
#if OBJC_API_VERSION >= 2
Method origMethod = class_getClassMethod(self, origSel_);
if (!origMethod) {
SetNSError(error_, #"original method %# not found for class %#", NSStringFromSelector(origSel_), [self className]);
return NO;
}
Method altMethod = class_getClassMethod(self, altSel_);
if (!altMethod) {
SetNSError(error_, #"alternate method %# not found for class %#", NSStringFromSelector(altSel_), [self className]);
return NO;
}
id metaClass = objc_getMetaClass(class_getName(self));
class_addMethod(metaClass,
origSel_,
class_getMethodImplementation(metaClass, origSel_),
method_getTypeEncoding(origMethod));
class_addMethod(metaClass,
altSel_,
class_getMethodImplementation(metaClass, altSel_),
method_getTypeEncoding(altMethod));
method_exchangeImplementations(class_getClassMethod(self, origSel_), class_getClassMethod(self, altSel_));
return YES;
#else
assert(0);
return NO;
#endif
}
Then, this method to illustrate the point:
- (void)doMagic:(id)sender {
static int i = 0;
i = (i+1) % 2;
if (i)
[[NSApplication sharedApplication] setApplicationIconImage:[NSImage imageNamed:NSImageNameBonjour]];
else
[[NSApplication sharedApplication] setApplicationIconImage:[NSImage imageNamed:NSImageNameDotMac]];
// any pre-populated image views have to be set to nil first, otherwise their icon won't change
// [imageView setImage:nil];
// [imageView setImage:[NSImage imageNamed:NSImageNameApplicationIcon]];
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert setMessageText:#"Shazam!"];
[alert runModal];
}
A couple of caveats:
Any image view already created must have setImage: called twice, as seen above to register the image changing. Don't know why.
There may be a better way to force the initial imageNamed: call with #"NSApplicationIcon" than how I've done it.
Try [myImage setName:#"NSApplicationIcon"] (after setting it as the application icon image in NSApp).
Note: On 10.6 and later, you can and should use NSImageNameApplicationIcon instead of the string literal #"NSApplicationIcon".

Resources