compare NSString using isEqualToString - iphone

Please, why doesn't isEqualToString work?
- (void)viewDidLoad {
[super viewDidLoad];
extern NSString* globalpassword;
if ([passwo.text isEqualToString: globalpassword]) {
res = [[XMLTestViewController alloc] initWithNibName:#"XMLTestViewController" bundle:nil];
res.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.view addSubview:res.view];
} else {
NSLog(#"faux");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"chikos"
message:#"Wrong Password" delegate:self cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[alert show];
[alert release];
}
}
the result is
2011-08-11 17:56:36.543 XMLTest[6389:207] pol
2011-08-11 17:56:36.544 XMLTest[6389:207] pol
2011-08-11 17:56:36.544 XMLTest[6389:207] faux

Take a look at your NSLog line; it should look like this :
NSLog(#"hi all freind");
(notice the # sign!)
EDIT This must mean that you're ignoring your compiler warnings! They're there for a reason; in this case, telling you that you're passing in the wrong parameter to NSLog.
I bet you're also getting warnings on the NSLog(cc) and NSLog(bb) lines as well?

The strings differ. Try logging bb and cc like this:
NSLog(#"bb: '%#', cc: '%#'", bb, cc);
With the single quotes you can see whether there is unexpected whitespace in either bb or cc.

Related

AlertView is running but not showing for didReceiveRemoteNotification

using the debugger I've ensured [alertView show]; is being called, but the dialog is not viewable, what else do I need to do to get it to show?
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if( [[userInfo objectForKey:#"aps"] objectForKey:#"alert"] != NULL)
{
NSString *msg = [[userInfo objectForKey:#"aps"] objectForKey:#"alert"];
if(msg != nil) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Usage Alert"
message:msg delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
[alertView show];
}
}
}
In your view controller .h file, you need to declare the <UIAlertViewDelegate> Like this, before the opening curly braces, encapsulated with greater than and less than symbols:
#import <UIKit/UIKit.h>
#interface YourViewControllersName : UIViewController <UIAlertViewDelegate> {
// your variable declarations are here...
}
// your method declarations are here...
Sorry everyone, this turned out to be an issue with my compiler/xCode not actually recognizing the code I had put in or something along those lines, the environment was broken.

Check if NSMutableArray contains text from textfield

I want to check if the text inserted in a textfield is also in my NSMutableArray.
So let's say my NSMutableArray has these objects: "Hey, Hello, No, Yes".
Then when a user enters the text: "Hello" i want there to appear a UIAlertView. I now have the following:
for (int slt = 0; slt < [zouten count]; slt++) {
if (zout.text = [zouten objectAtIndex:slt]) {
alert = [[UIAlertView alloc]initWithTitle:#"Goedzo!" message:[NSString stringWithFormat:#"Je hebt een zout gevonden"] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
}
}
[alert show];
But somehow the message appears with every word. what am i doing wrong?
When you compare like this:
if (zout.text = [zouten objectAtIndex:slt])
you are actually assigning instead of comparing so it will be TRUE always.Therefore instead of using =, you should compare like this:
if ([zout.text isEqualToString:[zouten objectAtIndex:slt]])
Your code should be:
for (int slt = 0; slt < [zouten count]; slt++) {
if ([zout.text isEqualToString:[zouten objectAtIndex:slt]]) {
alert = [[UIAlertView alloc]initWithTitle:#"Goedzo!" message:[NSString stringWithFormat:#"Je hebt een zout gevonden"] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
break;
}
}
You can use isEqualToString method to compare strings.
if([zout.text isEqualToString:[zouten objectAtIndex:slt]])
Alternatively you could make your code more concise by converting to a set and checking for the entry, e.g.
NSSet *set = [NSSet setWithArray: zouten];
if([set containsObject:zout.text]) {
...
}

XCODE - How to: UIAlertView (with a text field) on a loop until correct value entered into field?

I'm currently working in Xcode on an iOS app...
I've set up a UIAlertView (with a question as the message ) to pop up with a text field to retrieve a response.
The desired functionality is that upon entering an incorrect value into the text field, the UIAlert would loop... Until the correct response is entered. At this point, the UIAlert would be dismissed.
Heres what I have so far...
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
NSString* correctAnswer = #"2";
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Alarm"
message:#"1 + 1 ="
delegate:self
cancelButtonTitle: nil
otherButtonTitles:#"Continue", nil ];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField* answerField = [alert textFieldAtIndex:0];
answerField.keyboardType = UIKeyboardTypeNumberPad;
answerField.placeholder = #"answer";
[alert show];
// I feel like this would work, but I know it doesn't...
NSString *answerFieldString = answerField.text;
if ([answerFieldString isEqualToString: correctAnswer ])
{
[alert dismissWithClickedButtonIndex:-1 animated:YES];
}
}
I've done extensive google searching and can't come up with a solution... Any responses would be much appreciated!
try this...
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
NSString *answerFieldString = answerField.text;
if ([answerFieldString isEqualToString: correctAnswer ])
{
[alertView dismissWithClickedButtonIndex:-1 animated:YES];
}
}

UIAlertView and Core Data

Hi to all of stackoverflow,
i'm Italian so, i'm sorry for my bad English... :D
i tried to create a simple tableView for listing a few names and a number... all with core data.
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Track *track = [[self fetchedResultsController] objectAtIndexPath:indexPath];
if(YES == self.editing) {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Modifica nome" message:#"Prego, inserire di seguito il nuovo nome:" delegate:self cancelButtonTitle:#"Annulla" otherButtonTitles:#"Salva", nil];
newname = [[alert addTextFieldWithValue:track.name label:#"Nome"] text];
[alert textField].autocorrectionType = UITextAutocorrectionTypeNo;
[alert show];
[alert release];
[selectedTrack release];
} else {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Aggiungi minchiate" message:#"Sei sicuro di voler aggiungere una michiata a" delegate:self cancelButtonTitle:#"Annulla" otherButtonTitles:#"Salva", nil];
newname = [[alert addTextFieldWithValue:track.trackAbstract label:#"Minchiate"] text];
[alert textField].autocorrectionType = UITextAutocorrectionTypeNo;
[alert show];
[alert release];
[selectedTrack release];
}
}
I want to send the value of the textfield to my attributes that i've created first...
Need I put the code in this method?!
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex != [alertView cancelButtonIndex])
{
//HERE?!
}
}
And how i can set a new value on a core data attribute?
ANY help is appreciated... Thanks in advance!
Yes, you would put the code to grab the value from the text field in the alertView:willDismissWithButtonIndex:
method. Assuming you've created a model class for that entity (which I assume you have, as I see a class called Track in your code), you can set the value of an attribute by using dot syntax:
Track.someAttribute = newValue
Or without dot syntax, it would be [Track setSomeAttribute:newValue].
Also, note that UIAlertView's addTextFieldWithValue:label: is an undocumented method, which isn't a problem if this is an app you're planning to create just for yourself, but using it in an app submitted to the App Store will likely get it rejected.

Writing a function for UIAlertView?

I'm sick of writing basic UIAlertView's, ie:
UIAlertView *alert = [[UIAlertView alloc] initWith...]] //etc
Instead of doing this, is it possible to put all this in a "helper" function, where I can return the buttonIndex, or whatever an alert usually returns?
For a simple helper function I guess you could feed parameters for the title, message, I'm not sure whether you can pass delegates in a parameter though, or bundle info.
In pseudo-code, it could be like this:
someValueOrObject = Print_Alert(Title="", Message="", Delegate="", Bundle="") // etc
Any help on this would be great.
Thanks
In 4.0+ you can simplify the alert code using blocks, a bit like this:
CCAlertView *alert = [[CCAlertView alloc]
initWithTitle:#"Test Alert"
message:#"See if the thing works."];
[alert addButtonWithTitle:#"Foo" block:^{ NSLog(#"Foo"); }];
[alert addButtonWithTitle:#"Bar" block:^{ NSLog(#"Bar"); }];
[alert addButtonWithTitle:#"Cancel" block:NULL];
[alert show];
See Lambda Alert on GitHub.
This is what I wrote, when I got sick of doing the same:
-(void)alert:(NSString *)title withBody:(NSString *)message firstButtonNamed:(NSString *)firstButtonName {
[self alert: title withBody: message firstButtonNamed: firstButtonName withExtraButtons: nil informing: nil];
}
-(void)alert:(NSString *)title withBody:(NSString *)message firstButtonNamed:(NSString *)firstButtonName informing:(id)delegate {
[self alert: title withBody: message firstButtonNamed: firstButtonName withExtraButtons: nil informing: delegate];
}
-(void)alert:(NSString *)title withBody:(NSString *)message firstButtonNamed:(NSString *)firstButtonName withExtraButtons:(NSArray *)otherButtonTitles informing:(id)delegate {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: title
message: message
delegate: delegate
cancelButtonTitle: firstButtonName
otherButtonTitles: nil];
if (otherButtonTitles != nil) {
for (int i = 0; i < [otherButtonTitles count]; i++) {
[alert addButtonWithTitle: (NSString *)[otherButtonTitles objectAtIndex: i]];
}
}
[alert show];
[alert release];
}
You can't write a function that will display an alert and then return a value like a buttonIndex though, because that value-returning only occurs when the user presses a button and your delegate does something.
In other words, the process of asking a question with the UIAlertView is an asynchronous one.