`UIAlertView` appears multiple times - ios

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

Related

UIAlertView not dismissing?

I have a few ViewController subclasses inside a UINavigation controller. I have successfully used UIAlertViews elsewhere in the application, and I know how to set the delegate and include the correct delegate methods, etc.
In a ViewController with a UITableView, I have implemented a 'pull to refresh' with a UIRefreshControl. I have a separate class to manage the downloading and parsing of some XML data, and in the event of a connection error, I post a notification. The view controller containing the table view observes this notification and runs a method where I build and display an alert:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Connection Error" message:[[notification userInfo] objectForKey:#"error"] delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil];
alertView.alertViewStyle = UIAlertViewStyleDefault;
[alertView show];
The alert displays correctly, but the cancelButton is unresponsive - there is no way to dismiss the alert! Putting similar code (identical, but without the notification's userinfo) in the VC's viewDidLoad method creates an alert that behaves normally.
Is the refresh gesture hogging first responder or something? I have tried [alertView becomeFirstResponder]. I would be grateful for any advice…
Update: screenshot included… is this the right info? (can't embed this image for lack of reputation) http://i.stack.imgur.com/4CGqS.png
Edit
It seems like you have a deadlock or your thread is stuck waiting. You should look at your code and see what causes this.
Original answer which lead to update in OP
Make sure the alert is shown on the main thread:
dispatch_async(dispatch_get_main_queue(), ^{
//Open alert here
});
This isn't a solution per se, but I might try two quick things as you troubleshoot:
1) Hardcode some text in the UIAlert, rather than passing in the notification object. See if there is any change in behavior.
2) Try adding another button to the alert and an accompanying method to catch it. So you'll see if the delegate is getting ny buttons messages at all.
Try adding a tag to the alertView
alertView.tag = 0;
Then create the method alertView:clickedButtonAtIndex: in the view controller.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 0) {
[alertView dismissWithClickedButtonIndex:0 animated:YES];
}
}

Does a UIAlertView's Delegate have to be a view controller?

I've got an issue where an object that's creating a UIAlertView is sending a message back to its delegate, but the delegate object is crashing.
I've got it working OK in a couple of other instances, but what I'm trying to do in this case (and what's different from other similar questions) is that the object that instantiates the alert, and which acts as its delegate is not itself a view, but rather an object that is instantiated within a view. To wit, in the parent view:
#implementation
*MyCustomObject customObject;
-(void)viewDidLoad {
customObject = [[MyCustomObject alloc] init];
}
#end
And in the custom object:
-(void)DoCoolThings {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Do You Want To Do Cool Things"
message:#"...description of cool things..."
delegate:self
cancelButtonTitle:#"No Thanks"
otherButtonTitles:#"HELLS YES", nil];
[message show];
}
and
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
[self DoCoolThings];
} else {
[self GoAndCry];
}
}
When I do the alert view within a viewcontroller object, everything is fine. Doing it within this sub-object which itself has no view allows me to create the alert, but that object--which shouldn't be getting de-allocated based on the scoping--doesn't seem to want to continue to act as a delegate.
The error I'm getting is indeed a de-allocation message, but I feel strongly that this is not the problem because if I remove the alert, all the other stuff--specifically, it's a wrapper for a storekit purchase process--works fine, and all those delegate methods work happily.
I've got a solution which will allow me to move the Alert into the parent view's methods, but I was hoping not to have to. Is this limitation real, or is it my imagination? IE am I doing something else wrong?

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.

iOS and xcode: how to create a modal segue to a special view only the first time the app is used

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

Calling an IBaction in a alertView if statement

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).

Resources