Problem: The UIAlertView show \ninstead break a line, from a Json response.
Json Response:
{"error":"Line 1.\\nLine 2."}
Code
[self setLastError:[dictJsonResult jsonObjectForKey:#"error"]];
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Registration Failed"
message:[ParseHelper lastError] delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil] autorelease];
So Ismael already explained what's wrong; I tell you how to fix it:
[self setLastError:[[dictJsonResult jsonObjectForKey:#"error"]
stringByReplacingOccurrencesOfString:#"\\n" withString:#"\n"]];
The problem is that \\ translates to \ and it takes precedence because it comes first.
To explain a little further
ABC\\nDEF looks like:
ABC\nDEF
ABC\nDEF looks like:
ABC
DEF
Related
I'm trying to create a text box with multiple fields, but I'm having trouble getting the second to show (as a matter of fact, when I type the second field in, it causes my text box not to show up all together.)
Here's what I have:
-(IBAction)popupCheckIn {
//UIAlertView *alertCheckIn = [[UIAlertView alloc] initWithTitle:#"Check in" message:#"Please fill out the following to check in." delegate:self cancelButtonTitle:#"Check in." otherButtonTitles:#"Cancel.", nil];
//[alertCheckIn show];
UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:#"Check in" message:#"Please fill out the following fields to check in." delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles: nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * nameField = [alert textFieldAtIndex:0];
nameField.keyboardType = UIKeyboardTypeDefault;
nameField.placeholder = #"Your Name";
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * hostField = [alert textFieldAtIndex:1];
hostField.keyboardType = UIKeyboardTypeDefault;
hostField.placeholder = #"Host Name";
[alert addButtonWithTitle:#"Check in."];
[alert show];
When I run this code, I get an error that says "Thread 1: signal SIGABRT" and my pop up won't come up at all; when I have just the name field, it works fine.
What am I doing wrong with my second text field? Thanks!
I think that your error arises because that type of UIAlertView doesn't contain more than one UITextField, and when trying to access the second it raises a NSRangeException. This is according to the docs.
https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html#//apple_ref/occ/instm/UIAlertView/textFieldAtIndex:
I tried your code, the entire error message is:
2014-06-26 17:13:56.213 Testing1[2444:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'textFieldIndex (1) is outside of the bounds of the array of text fields'
The issue, is that you only have ONE UITextField with the UIAlertViewStyle set to UIAlertViewStylePlainTextInput. So this part of code ([alert textFieldAtIndex:1] is causing the crash).
Repeating the line alert.alertViewStyle = UIAlertViewStylePlainTextInput; won't create a new one.
The only way to get 2 UITextFields, is to use the UIAlertViewStyleLoginAndPasswordInput UIAlertViewStyle.
A way could be then to set the second one (like the first one) is like this:
[hostField setSecureTextEntry:FALSE];
But personally, I think that I don't recommend it. It may be blocked in the future.
Since we cannot custom really the existing UIAlertView since iOS7 (can't add subview), I'd suggest you create (or find in CocoaControls/GitHub) your own CustomAlertView-like.
You'll want to use alert with the alert style UIAlertViewStyleLoginAndPasswordInput. See this answer: UIAlertView with Two TextFields and Two Buttons
Say for example this is my C ( & Objective-C ) method as follows.
void ALERT(NSString *title, NSString *message,NSString *canceBtnTitle,id delegate,NSString *otherButtonTitles, ... )
{
// HERE I CAN ACCESS ALL THOSE ARGUMENTS
// BUT I AM NOT SURE How to access additional arguments, supplied using ... ?
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:title
message:message
delegate:delegate
cancelButtonTitle:canceBtnTitle
otherButtonTitles:// how to pass those params here?];
}
As you can notice that, I also have to pass those parameters in UIAlertView's init method. I am not sure how to send those parameters into otherButtonTitles. I can invoke this method by following ways.
ALERT(#"My Alert Title",#"Alert Subtitle",#"YES",viewCtr,#"No",#"May Be",#"Cancel",nil);
ALERT(#"Alert Title",#"Alert Subtitle",#"OK",viewCtr,#"Cancel",nil);
ALERT(#"Alert Title",#"Alert Subtitle",#"OK",viewCtr,nil);
ALERT(#"Alert Title",#"Alert Subtitle",#"OK",viewCtr,#"Option1",#"Option2",nil);
Sounds like you need to get know va_arg (and va_list, va_start, va_end).
Here's a tutorial on the subject.
Also, a fine Apple tech note entitled "How can I write a method that takes a variable number of arguments, like NSString's +stringWithFormat:?"
Edited to add:
Sounds like you want to do va_copy.
Ahh, here is a related question.
Hmmm ! I have gone through Wiki link
I also learned something from Michael' Answer in this post.
I have came up with following solution.
void ALERT(NSString *title, NSString *message,NSString *canceBtnTitle,id delegate,NSString *otherButtonTitles, ... )
{
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:title
message:message
delegate:delegate
cancelButtonTitle:canceBtnTitle
otherButtonTitles:nil
];
va_list args;
va_start(args, otherButtonTitles);
NSString *obj;
for (obj = otherButtonTitles; obj != nil; obj = va_arg(args, NSString*))
[alertView addButtonWithTitle:obj];
va_end(args);
[alertView show];
}
A noob question.
I have an alert:
"alertMessage" = "This is my message: 1 2 3. And another one: 5 6 7";
which I am displaying with:
NSString *asd = NSLocalizedString (#"alertMessage", #"");
NSString *alertTitle = NSLocalizedString (#"alertTitle", #"");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertTitle message:asd delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
How do I implement line breaks so "And Another one:" starts on the second line.
thank you!
Add line break characters (\n) to your message.
Add a \n in your string literal.
More details here
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Error"
message:#"Some failure message"
delegate:self
cancelButtonTitle:#"cancel"
otherButtonTitles:#"retry", nil
];
[alert show];
[alert release];
I got two warnings pointing at this block of code.
Unused variable 'alert'
Missing sentinel in function call
My code looks similar to all the examples online. Why is it broken?
This code works fine for me. Try cleaning all targets and building again to see if the error persists.
Because you call [alert show], you should not get an unused variable warning on alert.
Since you include nil after otherButtonTitles, you should not get a missing sentinel warning.
I have 2 pages with xib files. One is a login page and the other is a welcome page.
Here is the login function:
-(IBAction)login{
NSLog(#"login begin");
NSString *loginResponse = [self tryLogin];
NSLog(#"%#", loginResponse);
loginError.text = loginResponse;
NSUserDefaults *session = [NSUserDefaults standardUserDefaults];
[session setInteger:1 forKey:#"islogged"];
}
After the user is logged in successfully I need to redirect them to another xib file which is called Notifications. Now how exactly should I do this?
Also for some reason when I put an if statement inside the function, it doesnt go into the statements.
-(IBAction)login{
NSLog(#"login begin");
NSString *loginResponse = [self tryLogin];
NSLog(#"%#", loginResponse);
if(responseInt == #"2"){
NSLog(#"login error 2");
UIAlertView *alertURL = [[UIAlertView alloc] initWithTitle:#"Error!"
message:#"Username or password is wrong!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alertURL show];
[alertURL release];
///////////////////////////////////////////////////
}else if(responseInt == #"0"){
NSUserDefaults *session = [NSUserDefaults standardUserDefaults];
[session setInteger:1 forKey:#"islogged"];
}
}
As you can see I try to nslog something as soon as one of the conditions are met but it does not go into any of the statements (the loginResponse is sent back as 2).
How to change views
If you app is navigation-based then you can add new fullscreen views using method [self.navigationController pushViewController:controller animated:YES];.
Everywhere you can add new fullscreen view using next method : [self presentModalViewController:controller animated:YES];.
Issue with 'if-statement'.
When you want to compare to objects you should use one of following methods:
compare:, this method return one of following values: NSOrderedSame, NSOrderedDescending, NSOrderedAscending. For example, if ([responseInt compare:#"2"] == NSOrderedSame) {// equal}
'isEqual:', for example, if ([responseInt isEqual:#"2"]) {// equal}
or some specific comparators for concrete class
Don't forget that when you are comparing using == or > and etc you are comparing addresses of objects in memory.
Have you tried doing isEqualToString: instead of the equal sign? That is a more reliable string comparator. Also it seems strange that your login method is returning a string and not an integer. Is there a reason for that?