Tracking which UIAlert button is clicked to change UILabel - ios

I'm a complete beginner and looking to learn how to start coding to build my own apps.
I've been messing around with the classic "Hello World" design and trying to improve on it based on the limited knowledge i have managed to gather in the past week but i've got stuck and hope someone can help me out!
So far I have a UITextField for the user to enter their name, a label to display their name and a button to update the display. To help understand UIAlertViews better I have also included a Alert if the name entered is Chris (or chris) and another alert if the name entered is not Chris. All this is functioning fine so far.
I'm looking to track what button is pressed on my incorrectName UIAlertView and then update my UILabel to say "Chris" if they click the "I Want to be Called Chris" button. I have build another app similar to this before from a tutorial and I have copied across the code which i thought should function the same but it doesnt seem to work.
Here is my .m file so far
#import "Solo1ViewController.h"
#interface Solo1ViewController ()
#end
#implementation Solo1ViewController
#synthesize setMessage;
#synthesize userName;
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonTitle=([alertView buttonTitleAtIndex:buttonIndex]);
if ([buttonTitle isEqualToString:#"I Want To Be Called Chris!"])
{setMessage.text=#"Chris";}
}
-(IBAction)showMessage:(id)sender
{
setMessage.text=userName.text;
if ([userName.text isEqual:#"Chris"] || [userName.text isEqual:#"chris"])
{
UIAlertView *correctName;
correctName = [[UIAlertView alloc]initWithTitle:#":)" message:#"Great Name!" delegate:nil cancelButtonTitle:#"Bye Fellow Chris" otherButtonTitles:nil, nil];
[correctName show];
}
else
{
UIAlertView *incorrectName;
incorrectName=[[UIAlertView alloc] initWithTitle:#":(" message:#"Chris is a Better Name" delegate:nil cancelButtonTitle:#"No, Thanks" otherButtonTitles:#"I Want To Be Called Chris!", nil];
[incorrectName show];
}
}
-(IBAction)removeKeyboard:(id)sender{
[userName resignFirstResponder];
}
To me it seems as though this should work but I guess I'm probably missing something glaringly obvious or i'm doing it totally wrong.

Make sure to set the alert view's delegate to self! Right now you have the delegate set as nil.
Example:
incorrectName=[[UIAlertView alloc] initWithTitle:#":(" message:#"Chris is a Better Name" delegate:self cancelButtonTitle:#"No, Thanks" otherButtonTitles:#"I Want To Be Called Chris!", nil];

Related

Is there a way to stagger when multiple Alert Views show up in the same View Controller?

So I have two different UIAlertViews in the same view controller and both alerts can be triggered at the same time. When both alerts are triggered, both alerts pop up at the same time, with the alerts being layered on top of each other. Is there a way to stagger the alerts so that when the first alert comes up, the second alert will not pop up until the user dismisses the first alert? For my code, this is the format I'm using
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"ERROR!"
message:#"Error message here!"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alertView show];
try the following:
create two properties
#property (weak, nonatomic) UIAlertView *visibleAlertView;
#property (strong, nonatomic) UIAlertView *pendingAlertView;
every time when you want to present an alertview from your code make a check
UIAlertView *newAlertView = [[UIAlertView alloc] init...
if (self.visibleAlertView) {
self.pendingAlertView = newAlertView;
} else {
self.visibleAlertView = newAlertView;
[newAlertView show];
}
and finally:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (self.pendingAlertView) {
UIAlertView *newAlertView = self.pendingAlertView;
self.pendingAlertView = nil;
self.visibleAlertView = newAlertView;
[newAlertView show];
}
}
hope that helps :)
EDIT
you could even stack the pending alertviews:
#property (strong, nonatomic) NSMutableArray *pendingAlertViews;
...
self.pendingAlertViews = [NSMutableArray array];
before presenting an alertview:
UIAlertView *newAlertView = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
if (self.visibleAlertView) {
[self.pendingAlertViews addObject:newAlertView];
} else {
self.visibleAlertView = newAlertView;
[newAlertView show];
}
and in dismiss:
if (self.pendingAlertViews.count > 0) {
UIAlertView *av = self.pendingAlertViews.firstObject;
[self.pendingAlertViews removeObjectAtIndex:0];
self.visibleAlertView = av;
[av show];
}
hope it helps :)
Why don't you make a class level variable that indicates an alertView is open. Then before you open one you check that variable and if it's set you don't pop up the second one. Instead you could have it set another variable that indicates the second box should pop up. Then in the - alertView:clickedButtonAtIndex: method you can pop up the second one if the second variable is set.
I think I am pretty late for this but still posting as It might be useful for someone looking for this.
I have created a AQAlertAction subclass for UIAlertAction. You can use it for staggering Alerts, the usage is same as you are using UIAlertAction. All you need to do is import AQMutiAlertFramework in your project or you can include class also (Please refer Sample project for that). Internally It uses binary semaphore for staggering the Alerts until user handle action associated with current alert displayed. Let me know if it works for you.

iOS: How do I get text from a text field and display it in an alert?

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];
}

iOS UIAlertview clear text and disable OK

I'm a bit new to iOS development, and right now am working on some simple UI-related stuff. I have a UIAlertView that I'm using at one point to allow the user to enter some text, with simple Cancel and OK buttons. The OK button should be disabled if the text field is blank.
I added to my UIAlertViewDelegate an alertViewShouldEnableFirstOtherButton function, so the OK button would disable when there's no text, and I also set the UIAlertView's UITextField to have clearOnBeginEditing true, so the previous text would be gone every time I displayed the alert. Each of these things works perfectly on their own. Unfortunately, it seems like the AlertView is checking whether or not to enable the OK button before the text field is cleared, so when they're put together it comes up enabled. Below should be about the minimal code needed to reproduce.
-(void)viewDidLoad
{
textEntryBox = [[UIAlertView alloc] initWithTitle:#"Name" message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[textEntryBox setAlertViewStyle:UIAlertViewStylePlainTextInput];
[textEntryBox textFieldAtIndex:0].clearsOnBeginEditing = YES;
}
-(IBAction)functionTriggeredByOtherLogic
{
[textEntryBox show];
}
-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
if(alertView == textEntryBox)
{
if([[alertView textFieldAtIndex:0].text length] > 0)
{
return YES;
}
else
{
return NO;
}
}
return YES;
}
So, ultimately, my question is this: am I doing something completely against the natural iOS way of doing things here? Is there a better way to do this? Should I just ignore the clearsOnBeginEditing property of the UITextField, and manually clear the Text property before showing the UIAlertView?
Try to set the textfield delegate to self
[[textEntryBox textFieldAtIndex:0] setDelegate:self]
and implement this method :
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField setText:#""];
}
I'm also having a UIAlertView with a textField to fill-in in my app, and it works for me
Using an alert view for this is probably a bit much. It might be easier if you use the master-detail paradigm and just push a new view controller where you can enter your values.

How to set a delegate when ViewController already has a delegate?

I have an iPhone app which sends an email. If the "To:" address is not set, I display an Alert (UIAlertView). At the present time, I do not check for the user tapping OK, I just go on my merry way! :D
I am getting the following error when tapping OK on the Alert:
wait_fences: failed to receive reply: 10004003
I believe it's because I don't handle the tapping of OK and it's still showing when the app is doing something else. So, after doing some research on SO and Google, it appears I have to have this:
- (void) Alert: (NSString *) title andData: (NSString *) errorMsg {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: title
message: errorMsg
delegate:nil
cancelButtonTitle: #"OK"
otherButtonTitles: nil];
[alert show];
return;
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(#"button was pressed");
}
My problem is that I can't figure out how to set up the delegate for this. I already have a delegate:
#interface ReportViewController : UIViewController <UIWebViewDelegate> {
How do I set the delegate so the tap of the OK button is handled, thus removing the error?
Inside the angle brackets, you can provide a comma-separated list of protocols.
#interface ReportViewController : UIViewController <UIWebViewDelegate, UIAlertViewDelegate> {
Now you can implement both sets of methods. And don't forget to set the alert view's delegate to self.
Your delegate looks like nil as per your code 'delegate:nil' in the question. You need to change it to 'delegate:self'.

UIAlertView in appdelegate.m does not work

I have been googling this problem for almost a whole day now, without getting any closer to a solution, so i would like to ask you guys.. :)
I'm working on an iOS app, which should connect to a mbed over WiFi and give the user a dialog if it connects and if it doesn't and if not, then give the user the possibility to retry.
My problem is now that i have implemented the connecting method in appdelegate.m and it is from here I would like to show the alerts..
The alerts it self works fine, but I have problems detecting when a button is pressed, the clickedButtonAtIndex is not being called.
I have added the UIAlertViewDelegate in the appdelegate.h, like so:
#interface AppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate, UIAlertViewDelegate>
and have set the delegate to self, in the alertview, like so:
alert_NOT = [[UIAlertView alloc] initWithTitle:#"Not connected!" message:message_to_user delegate:self cancelButtonTitle:#"Try again" otherButtonTitles: nil];
[alert_NOT show];
[alert_NOT release]
and the clickedButtonAtIndex looks like
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(#"test");
}
So I would love to see the word "test" in the log when a button is pressed in the alertview, but nothing happens.
Update:
Tried implementing it in my "FirstViewController.m" and there it works :S but I would very much like to have it in the appdelegate.m if possible..
I'm currently looking into a similar implementation and would like to share an idea I had with you: perhaps using an NSNotification that fires when your delegate's conditions are met, which can be listened for in your VC(s) and handled appropriately, with an alert view, at the top of the stack.
#interface urAppDelegate : NSObject <UIApplicationDelegate,UIAlertViewDelegate>
If you synthesized the alert_not then use it like this with self:
self.alert_NOT = [[UIAlertView alloc] initWithTitle:#"Not connected!" message:message_to_user delegate:self cancelButtonTitle:#"Try again" otherButtonTitles: nil];
[alert_NOT show];
[alert_NOT release];
You should use the alertViewCancel method for this.
- (void)alertViewCancel:(UIAlertView *)alertView
{
NSLog(#"text");
}
Define as below:
#define appDelegate ((AppDelegate*)[UIApplication sharedApplication].delegate)
and alert as:
UIAlertView *alert_NOT = [[UIAlertView alloc] initWithTitle:#"Not connected!" message:message_to_user delegate:appDelegate cancelButtonTitle:#"Try again" otherButtonTitles: nil];
[alert_NOT show];
Here set delegate as defined keyword, i.e., appDelegate.
Hope this helps.

Resources