I'm attempting to get a simple alert message to display in my app. I am using the code recommended by Apple, but every time the Cancel button displays without its text. I don't believe I have any other code that could override the default display of the alert.
Screenshot can be seen here.
Here's my code to display the alert:
NSString *alertMessage = [NSString stringWithFormat:#"No results found for '%#'.", #"TEST"];
UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:#"Search Error"
message:alertMessage
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil];
[theAlert show];
When I use NSLog(#"%#", [theAlert buttonTitleAtIndex:0]); to log the title, it outputs "Cancel" just fine.
I want to comment, but I have low reputation for it... Anyway, I had simmilar issue with UIButton, and I figured out, it was bug in Xcode 6. Have you tried Xcode 5?
Related
I wanted to use UIAlertView to show photo. I´m using code as below for showing the alert, however it doesn't work. It shows the title, some space and button OK nothing more. I have no idea if I´m doing something wrong, because I´m new in iOS.
-(IBAction)ButtonPressed:(id)sender
{
UIAlertView *av = [[UIAlertView alloc] initWithTitle:#"titel"
message:nil
delegate:nil //or self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(180, 10, 85, 50)];
[imageView setImage:#"dog.png"];
[av setValue:imageView forKey:#"accessoryView"];
[av show];
}
#Popeye is correct what he said : UIAlertView is meant to be used as-is and you shouldn't be messing with the view hierarchy this will get your app rejected from the Apple App review process. Please read Apple documentation regarding UIAlertViews. So you should NOT be using setValue: forKey:#"accessoryView" and/or addSubview:. Specific section below
Subclassing Notes
The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.
Solutions
1) Use 3 party such as CNPPopupController and many other available check this link
OR
2) create of your own.
guys. This is totally going to sound like I'm asking for you to do my homework for me, but I'm not. My employer FINALLY gave me this sweet new MacBook Pro. One of my tasks will include some iOS development. I'm pumped about it and I'm trying to dive right in to learning, so I'm making a silly little application that just lets me see how to interact and write some code. This morning's task is to take some text from a text field and display it in an alert. I've done lots of googling and found lots of things -- even stuff on StackOverflow -- but a lot of it is over my head or not exactly relevant. So, I'm hoping someone can show me what I've done wrong.
Here's my code for the text field:
-(IBAction)showInputMessage:(UITextField *)textField
{
if ([textField.text isEqualToString:#""])
{
return;
}
UIAlertView *helloEarthInputAlert = [[UIAlertView alloc]
initWithTitle:#"Name!" message:[NSString stringWithFormat:#"Message: %#", textField.text]
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
// Display this message.
[helloEarthInputAlert show];
}
And then I connect that text field to showInputMessage and run it in the iPhone simulator, but nothing happens when I enter text and click "Enter".
Thanks in advance. I've only been playing with this language since last night.
Jeremy
Set Delegate for UITextView.
First declare the delegate:
#interface YourViewController ()<UITextFieldDelegate>
Second set to self
self.textView.delegate = self;
Use this method:
-(void)textViewShouldReturn:(UITextField *)textField
{
if ([textField.text isEqualToString:#""]){
return;
}
UIAlertView *helloEarthInputAlert = [[UIAlertView alloc]
initWithTitle:#"Name!" message:[NSString stringWithFormat:#"Message: %#", textField.text]
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
// Display this message.
[helloEarthInputAlert show];
}
To add a property write the following code in your h file:
#property (weak, nonatomic) IBOutlet UITextView *textView;
To connect it your text field go to the storyboard and click its view controller. Next go to the Connections Inspector(All the way on the right). Under outlets drag the circle next to textView to your textView.
Have you linked the UITextField in the xib file or the storyboard?
I see now that you have a textField as a parameter. You won't get anything like that;
Do this in .h
IBOutlet UITextField *textFieldTest;
then link it in the xib or storyboard
-(IBAction)showInputMessage
{
if ([textFieldTest.text isEqualToString:#""])
{
return;
}
UIAlertView *helloEarthInputAlert = [[UIAlertView alloc]
initWithTitle:#"Name!" message:[NSString stringWithFormat:#"Message: %#", textFieldTest.text]
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
// Display this message.
[helloEarthInputAlert show];
}
I'm creating an app, and at one moment i want to ask the user opinion with a custom UIAlertView. After a lot of researches, and reading about this subject, i'm a little confused about some things...
What objects (UITextfields, UIImages...) could we add to an UIAlertView ?
Because i found this :
The UIAlertView class is intended to be used as-is and does not
support subclassing. The view hierarchy for this class is private and
must not be modified.
But this could have been accepted for example :
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"title" message:#"msg" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
[alertView addSubview:txtField];
[alertView show];
[alertView release];
(See this link for more informations)
So for example, if i don't want the blue-box with message, and title parameters, could i only do this code :
UIAlertView *alert = [[UIAlertView alloc] init];
[alert addSubview:a_UIButton];
[alert addSubview:a_UIImageview];
[alert show];
So with this sort of code i could get the advantage (if it's work !) of put in pause mode all the app', and all would be custom. But is it possible ? Could it be reject ?
Thanks a lot !
Instead of trying to customize "UIAlertView", why not just create your own custom AlertView (from "UIView") and add that as a subview when you want to display it?
We don't know if (or how) Apple might change the internal architecture of UIAlertView in future versions of iOS (e.g. iOS 7) that would break all the various customizations done to it in all the apps that have gotten away with it so far. That's why Apple put up that warning in their documentation.
I would recommend instead that you make a custom UIView, and just display that with a similar animation to the UIAlertView. Then, you can add whatever UI elements you'd like. If the docs say the view hierarchy is private, I'd leave it alone.
If all you want to do is add a text field to the alert view, that functionality is supported in iOS 5 and later using:
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
I have this code
- (IBAction)save:(id)sender {
self.imageView.image = [UIImage imageNamed:#"loading.gif"];
[self.view setUserInteractionEnabled:NO];
MBProgressHUD *hudSave = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
hudSave.labelText = #"Saving...";
NSLog(#"save");
UIAlertView *deco;
if (!isVideo) {
UIImageWriteToSavedPhotosAlbum (imageView.image, nil, nil , nil);
deco = [[UIAlertView alloc]initWithTitle:#"Save" message:#"Your photo has been saved." delegate: nil cancelButtonTitle:#"oK" otherButtonTitles:nil];
}else{
//UIAlertView *explain;
//explain = [[UIAlertView alloc]initWithTitle:#"Wait during processing" message:#"Your video is being filtered, this process may be long, depending of both video and device. Please do not close this app until task is finished." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
UIAlertView *explain;
explain = [[UIAlertView alloc]initWithTitle:#"Wait during processing" message:#"Your video is being filtered, this process may be long, depending of both video and device. Please do not close this app until task is finished." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[explain show];
[self InitialisationRefiltrage:urlVideo];
//[self InitialisationRefiltrage:urlVideo];
deco = [[UIAlertView alloc]initWithTitle:#"Save" message:#"Your video has been saved." delegate: nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
}
[MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
[deco show];
[self.view setUserInteractionEnabled:YES];
}
The problem part is if my file is a video, it goes directly too "initialisationRefiltrage" (who is working fine) but without displaying the MBProgressHUD and the alert view explain, and after my video traitement, it display everything (explain, deco, and the MBProgressHUD) at the same time.
I try something with dispatch, thread, etc... But i think a don't do it correctly, so can you please give me a clue too how to do that.
Have a nice day.
Put the code [self InitialisationRefiltrage:urlVideo] in the delegate method of your UIAlertView so that it is executed only when the alert has been displayed and user has tapped on a button of the alert.
You may also use instead some third-party UIAlertView subclasses that uses completion blocks to make your code only execute when the alert is dismissed. See my class that does this for example.
Besides, you should respect coding conventions and use a method name beginning with a lowercase letter to make your code more readable.
The UI is updated in the "run loop".
The calls you're making tell iOS to display some views (alert, MUD...) and it'll do that on the next run through the loop.
What you need to do is wait for the user to respond to the alert before continuing. You do this by setting yourself as the UIAlert's delegate, then responding to the event:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
...
}
There are also libraries available that allow you to pass a block to the alert view, thus simplifying the whole thing. (https://github.com/jivadevoe/UIAlertView-Blocks, for example)
P.S. I see that you're new to Stack Overflow - please tick my answer if you're happy that it has responded to your question...
I am trying to make a iPad application which information will popup inside the UINotification. But the default size of the notification is relatively small. Is there any way i can increase the size of the notification?
-(IBAction) player1:(id)sender{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Player1" message:#"Long message"delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil, nil];
[alert show];
}
I tried to add this code,
alert.frame = CGRectMake(0,0,500,500);
but the text inside does not follow.
Is there any way to solve this?
Thanks
You can't change an UIAlertView :( It's deliberately designed to force you to be brief!
If you want to display a large amount of data, your best option is to create a custom UIView and show that instead.