Me and my buddy are working on an app, we're total newbies but have come a long way with books and goggling.
We're stuck now on this thing. We have a bunch of texfields that we have clear button linked to it with this action, but then we want that action to be called if you click "Yes" on one of the alert view buttons.
- (IBAction)clearText:(id)sender {
Spelare1Slag1.text = #"";
Spelare1Slag2.text = #"";
}
We also have this alert view:
alertDialog = [[UIAlertView alloc]
initWithTitle: #"Warning"
message: #"Do you want to delete?"
delegate: self
cancelButtonTitle: #"No"
otherButtonTitles: #"Yes", nil];
- (void)alertView: (UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"No"]) {
}
else if ([buttonTitle isEqualToString:#"Yes"]){
Spelare1Slag1.text = #"";
}
}
So this is how we think we should do it, but we don't know what to put in the else if statement. We want the textfields to clear when you press the "yes" button in the alert view, and not when you press "no"
Thanks in advance!
The clearText method, I'm assuming, is a custom method you created to delete the text in both the fields right? So instead of it being an IBAction, it should be a void method :
- (void)clearText {
Spelare1Slag1.text = #"";
Spelare1Slag2.text = #"";
}
Now all you need to do in your UIAlertView delegate method, is call the clearText method :
- (void)alertView: (UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"Yes"]){
[self clearText];
}
}
Hope this helps
You could dismiss the AlertView in case of user has clicked NO If I understand question properly.
You could dismiss the alertview like this
[alertView dismissWithClickedButtonIndex:0 animated:YES];
but make sure to see if NO has index 0 or 1, if you are not sure then just do like this
[alertView dismissWithClickedButtonIndex:nil animated:YES];
Methods of type IBAction are just like any other method, and you can call them in your code directly. In fact, IBAction is simply a macro evaluating to void. Usually, you'll pass a nil sender argument when it needs to be called outside of the context of a target / action event being triggered.
[self clearText:nil];
Why do you need to check the actual button text? Checking the button index is the most efficient way to go. If you have two fields in your alertView, just check if the index is 0 or 1 and your good to go. Your doing extra work checking the actual text.
And btw, just do a check for the index of the YES button if you don't need to do anything specific when they press no. (Don't check both indexes if you don't need to).
Related
Am new to AlertViews with actions. Have set mine up as in the examples I found here including setting the delegate method in my .h, but when I debug find that it is not reaching my clickedButtonAtIndex method. Probably missing something simple :-/ Any help solving this would be appreciated, thanks.
Here's my code:
.h file
#interface LineDetailViewController : UIViewController <UIAlertViewDelegate>
. m file:
- (IBAction)removeLineButton:(UIButton *)sender {
NSString * requestSubmitText = [NSString stringWithFormat:#"Hey Bro are you sure you want to remove this line you created, it will also remove all the reviews attached and can not be undone?"];
UIAlertView *removeLineRequest = [[UIAlertView alloc] initWithTitle:#"WARNING!!"
message:requestSubmitText
delegate:nil
cancelButtonTitle:#"Remove It"
otherButtonTitles:#"Cancel",nil];
[removeLineRequest show];
- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex{
//if (buttonIndex == [alertView cancelButtonIndex]){
if (buttonIndex == 0){
NSLog(#"Cancel button pressed");
}else{
NSLog(#"other button pressed");
}
}
You have to set delegate to self
UIAlertView *removeLineRequest = [[UIAlertView alloc] initWithTitle:#"WARNING!!"
message:requestSubmitText
delegate:self // <- here
cancelButtonTitle:#"Remove It"
otherButtonTitles:#"Cancel",nil];
Call removeLineRequest.delegate = self.
While creating your alert view set your delegate to self . And you have already implemented the UIAlertViewDelegate.
To solve your problem, just set the delegate to self instead of nil.
Some additional info,
UIAlertView and UIAlertViewDelegate have been deprecated. As per the documentation,
UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead.
Relevant tutorial here
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.
I display an alert view in the didConnect method for bluetooth. For some reason, it fires four times. I'm trying to bandaid it and it is not working too well. Basically, I put the alert view in a method of its own, and call that method in didConnect. That's when it fires four times. I'm trying to set it up to only fire once. What I tried to do was set the alert views method to return a TRUE value. Then I do this:
if ([successfulConnection self] == FALSE) {
[self successfullConnection];
}
This works great the first time, but then the method is set to TRUE for the remainder of the time. I have the feeling that if I set it back to equal FALSE at the end of the if statement, then it will fire four times and I'll be right back where I started. Does anyone know how to change the above code to have it only fire once when it tries to fire four times?
Also tried replacing the above code with this in my didConnect, but it never fired at all:
[successfulConnection self];
if (successfulConnection.visible == YES) {
[successfulConnection dismissWithClickedButtonIndex:0 animated:YES];
}
If you call successfulConnection from your didConnect method, I think this should work (myAlert is the property name for the alert view):
-(void)successfulConnection {
if (! self.myAlert) {
self.myAlert = [[UIAlertView alloc]initWithTitle:#"ttile" message:#"message" delegate:self cancelButtonTitle:#"cancel" otherButtonTitles: nil];
[self.myAlert show];
}
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
self.myAlert = nil;
//do whatever with the result
}
The simplest thing to do is to just have a boolean that gets set to true when the UIAlertView is displayed, and then false when the UIAlertView is dismissed. Then, whenever you are going to show the UIAlertView, first check if it is already displaying.
These are methods you can use according to your requirement:
EDIT : Perfect way... if you dont want to upload your app on App Store
To know that alertView is currently visible or not.
Usage : Display alertView only if necesary other its already present.
-(UIAlertView *)getLastAlertView
{
Class UIAlertManager = objc_getClass("_UIAlertManager");
UIAlertView *topMostAlert = [UIAlertManager performSelector:#selector(topMostAlert)];
return topMostAlert;
}
Dissmiss any alertView present which you don't know.
Usage : dissmiss all alertView and then present new one
-(void)dissmissLastAlert
{
Class UIAlertManager = objc_getClass("_UIAlertManager");
UIAlertView *topMostAlert = [UIAlertManager performSelector:#selector(topMostAlert)];
if (topMostAlert) {
[topMostAlert dismissWithClickedButtonIndex:0 animated:YES];
}
}
In my add contact page, am validating my email textfield , phone number fields, etc. And am checking valid email on clicking all other textfields. So when i click on the phone number field, it will check email validity and show an alertview if email is invalid. So when i click ok button in the alertView, the cursor should go to the email textfield instead of being in the phone number field.. Can anyone help on this..?
for making any textfield first responder. and you can do this in your alertview delegate.
[yourTxtField becomeFirstResponder];
in alertview's delegate method,
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if([alertView isEqual:emailAlert])//emailAlert should be the instance variable
{
if(buttonIndex == 0)//assumes ok button index is 0
{
[textfield becomeFirstResponder];
}
}
}
Make your view controller implement the UIAlertViewDelegate, and add the current class as delegate to the shown alert view. In – alertView:clickedButtonAtIndex: make phone text field resign first responder and make the email text field befome first responder.
You can set AlertView's delegate and on click of "OK" button ,
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
[txtEmail becomeFirstResponder]; // your "Email" textField will get focused
}
}
Try this way.. HTH :)
Make sure that you class conforms to UIAlertViewDelegate protocol and show the alert like
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Your Message" delegate:self cancelButtonTitle:#"cancel " otherButtonTitles:#"OK", nil];
[alert show];
[alert release];
implement the delegate method like
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0)//ok button index is 0
[textFieldEmail becomeFirstResponder];
}
I want to make a "terms and conditions" screen that pops up the very first time that app is opened. On this view I would add a button that says "agree," and upon clicking it the code would execute:
[self dismissViewControllerAnimated:YES completion:nil];
...and would go to the first view of the app.
I am currently using a Tab Bar Controller that has 4 ViewControllers. So basically, I just need to have some method in viewWillAppear on my first ViewController that checks for an NSUserDefault key:value. The first time the app opens, it will be zero. After they click agree, I'll set it to 1 and the bit of code would never execute again.
Can you please offer some code to accomplish the task of routing the view from the firstViewController's view to this alternate view controller upon loading the app?
Thanks!
In the viewWillAppear method in your FirstViewController, check NSUserDefaults then present your TermsViewController. After user click agree in TermsViewController, set NSUserDefaults then call
[self.presentingViewController dismissModalViewControllerAnimated:YES]
The use of the popover window can get complicated. Try something like the following if you have little experience with Objective-C.
- (void)viewDidLoad {
if ([termsvalue == 0]) {
NSString *msg = [NSString stringWithFormat:#"Do you agree with the terms of use?"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"- Confirmation -"
message:msg
delegate:self
cancelButtonTitle:#"Disagree"
otherButtonTitles:#"I agree", nil];
[alert setTag:100];
[alert show];
}
}
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView { // Validation
if ([alertView tag] == 100) {
return YES;
}
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { // Go
if ([alertView tag] == 100) {
if (buttonIndex == 1) {
// The user has agreed
}
}
}