How to dismiss alertview when click on the button in that alertview - iphone

I am new to iOS.
I am working on alertviews. Here is my code. Here there are 2 alertviews: successfulallert and unsuccessfulallert for login page. I am using alertview delegate also here, it will work for both alertviews but I want to work only for successful alertview and navigation should be done only for successful alertview. If anybody knows this please help me.
NSString *responseOfResult = [[NSString alloc]initWithString:[result response]];
NSRange match;
// NSLog(#"string= %#", str);
match = [responseOfResult rangeOfString: #"successful"];
if(match.location == NSNotFound)
{
UIAlertView *unsuccessfulAllert = [[UIAlertView alloc]
initWithTitle:#"Alert"
message:responseOfResult
delegate:self
cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[unsuccessfulAllert show];
}
else {
UIAlertView *successfulAllert = [[UIAlertView alloc]
initWithTitle:#"Message" message:#"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[successfulAllert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0){
[[self navigationController]pushViewController:registerUserScreen animated:YES];
}
}

Why don't you put "OK" as cancelButtonTitle? Everything will be handled automatically.
UIAlertView *successfulAllert = [[UIAlertView alloc]
initWithTitle:#"Message" message:#"Login successful." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[successfulAllert show];

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0){
//POP here with this:
[self.navigationController pushViewController:addItemView animated:NO];
}
}

NSString *responseOfResult = [[NSString alloc]initWithString:[result response]];
NSRange match;
// NSLog(#"string= %#", str);
match = [responseOfResult rangeOfString: #"successful"];
if(match.location == NSNotFound)
{
UIAlertView *unsuccessfulAllert = [[UIAlertView alloc]
initWithTitle:#"Alert"
message:responseOfResult
delegate:self
cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[unsuccessfulAllert setTag:1];
[unsuccessfulAllert show];
}
else {
UIAlertView *successfulAllert = [[UIAlertView alloc]
initWithTitle:#"Message" message:#"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[successfulAllert setTag:2];
[successfulAllert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag == 2)
{
[[self navigationController]pushViewController:registerUserScreen animated:YES];
}
else
{
//[[self navigationController]pushViewController:registerUserScreen animated:NO];
// OR
return;
}
}

You have many ways to correct your code, the first and very common is to use the tag property (integer) of the UIView. Since UIAlertview inherits from UIView, it has the tag property, so each time you want create an alert (or a view), set the tag and the check your condition like:
...
alert.tag=1;
[alert show];
then to know wich alert is calling the callback:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag==theTagOfYourAlert){
//do your stuff
}
}
another way, in your case, could be:
if([alertView.title isEqualToString:#"Alert"]){
//do your stuff
}
}

Add tag to the two alert views and check for tag in alert view delegate.
Sample code:
NSString *responseOfResult = [[NSString alloc]initWithString:[result response]];
NSRange match;
// NSLog(#"string= %#", str);
match = [responseOfResult rangeOfString: #"successful"];
if(match.location == NSNotFound)
{
UIAlertView *unsuccessfulAllert = [[UIAlertView alloc]
initWithTitle:#"Alert"
message:responseOfResult
delegate:self
cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[unsuccessfulAllert setTag:1];
[unsuccessfulAllert show];
}
else {
UIAlertView *successfulAllert = [[UIAlertView alloc]
initWithTitle:#"Message" message:#"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[successfulAllert setTag:2];
[successfulAllert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag==2 && buttonIndex == 0){
[[self navigationController]pushViewController:registerUserScreen animated:YES];
}

Yes the delegate would work for both the alertviews but you can assign a tag to each alertview Object and check for the tag in the delegate and then perform event if the tag for that particular AlertView onject matches.If u need code , i will provide.

For things like Login status updates, you might want to have the "Login Successful" message disappear automatically. Try this instead:
https://github.com/camclendenin/flashbox
This works nicely and comes in handy for situations like this. Plus you don't have to deal with all the clutter involved with UIAlertViews.

Related

Alert view is working in one class, but not the other

I have a class name viewController and it has the following code below and it works fine. However, when I call check from my subclass controller it doesn't work the way I want it to.The UIAlertView shows up, but it isn't able to detect when button index 0 is touched.Any workaround for this.
-(void)check{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"You Lose!"
message:#"Play Again?"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:#"OK", nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) { // and they clicked OK.
ViewController*myNewVC = [[ViewController alloc] init];
[self presentModalViewController:myNewVC animated:NO];
}
}
Add UIAlertViewDelegate in your subclass .h file like bellow..
#interface yourSubClassViewController :UIViewController <UIAlertViewDelegate>{
/// your code
}
#end
The Cancel button will be the index "0", Try for index==1.
Use this method:
-(void)check{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"You Lose!"
message:#"Play Again?"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:#"PopUp", nil];
[alert show];
}
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSLog("Cancel Button clicked);
}
else if (buttonIndex == 1) {
ViewController*myNewVC = [[ViewController alloc] init];
[self presentModalViewController:myNewVC animated:NO];
}
Don't use cancel button. Use two buttons otherButtonTitles and use the buttons index == 1 and button index == 2 . That may help.
You need to set the delegate to your subclass.
-(void)check:(id)delegate{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"You Lose!"
message:#"Play Again?"
delegate:delegate
cancelButtonTitle:#"OK"
otherButtonTitles:#"OK", nil];
[alert show];
}
and you call it [self check:self];

How to make the UIAlertView call itself again?

Below is the scenario.
I have created a UIAlertView in viewDidLoad of the Controller in which I display the Alert.
loginAlert = [[UIAlertView alloc] initWithTitle:#"Check"
message:#"Ok"
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[loginAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput];
I call it as [alert show] and it is displayed.
When user press Ok, it goes to textEndEditing delegate method and from if nil/worng value was entered, I call [alert show] again.
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if ([textField.text length] > 0)
{
}
else
{
NSLog(#"Checking");
[loginAlert show];
}
}
But its not displayed again. Please tell me what to do?
Instead of delegate:nil, put delegate:self
loginAlert = [[UIAlertView alloc] initWithTitle:#"Check"
message:#"Ok"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[loginAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput];
First change your alert view delegate to self.
loginAlert = [[UIAlertView alloc] initWithTitle:#"Check"
message:#"Ok"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[loginAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput];
[loginAlert show];
Then in the alert view delegate method check the length of the text and show the the alert again if empty.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
if ([[loginAlert textFieldAtIndex:0].text length] > 0)
{
}
else
{
NSLog(#"Checking");
[loginAlert show];
}
}
}
How about making it simple .. ?
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if ([textField.text length] > 0)
{
}
else
{
NSLog(#"Checking");
loginAlert = [[UIAlertView alloc] initWithTitle:#"Check"
message:#"Ok"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[loginAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput];
[loginAlert show];
}
}
Try this:
loginAlert = [[UIAlertView alloc] initWithTitle:#"Check"
message:#"Ok"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[loginAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput];
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
[yourTextField resignFirstResponder];
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
if (textField==yourTextField) {
if ([yourTextField.text isEqualToString:#""] || yourTextField.text == nil) {
[yourAlert show];
}
}
}
Also see this for your problem:
Keep UIAlertView displayed
uialertview called more than once
Set delegate self instead of nil and implement my code.
loginAlert = [[UIAlertView alloc] initWithTitle:#"Check"
message:#"Ok"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[loginAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput];
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)
{
[self.view endEditing:YES];
}
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0)
{
// Your code write in this method and show your alert
}
else
{
[loginalert show];
}
}
i hope this code useful for you.

How to show a UIAlertView upon selection of an item in UIPickerView

In my picker view i have a "Custom" option which should popup a UIAlertView for the user to enter a new value, the value is saved in the plist source of the picker for future reference. xxxEditingDidBegin is being called repeatedly (never ending).
I presume its because my UIAlertView is triggering the picker to close.
How should I have done this?
- (IBAction)serviceTypeFieldEditingDidEnd:(UITextField *)sender
{
UIPickerView *picker = [sender.inputView.subviews objectAtIndex:0];
NSString *selText = [serviceTypeArray objectAtIndex: [picker selectedRowInComponent:0]];
sender.text = selText;
if (NSOrderedSame==[selText compare:#"Custom"])
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Custom Role"
message:#"Enter Role Title"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[alertView show];
}
}
Implement method like
- (IBAction)doSelectDate:(UIDatePicker *)sender
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Hi" message:#"AlertView is shwoing" delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
and connect above method with ValueChanged Event of UIPickerView;
I fixed it like this
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
NSString *rowString = [serviceTypeArray objectAtIndex:row];
if ([rowString compare:#"Custom"] == NSOrderedSame)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Custom Role"
message:#"Enter Role Title"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[alertView show];
}
else
{
_serviceType.text = rowString;
}
}

How to create different uiactionsheet for two buttons ?

Hi here i developing small application. In a subclass screen i have two buttons. When i press the first button, it will shows four actionsheet. When i press second button it wil shows five actionsheet. I was successfully shows it. But i cant set second button actions of five actionsheet. In my code the when i press second button of first actionsheet, it wil actioned first button of first actionsheet. Here i want set actions for individual actionsheets. Pls help me. Here is my code is
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ( indexPath.row == 0)
{
UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"Edit" otherButtonTitles:#"Remove", #"Sell",#"Scrap", nil];
popupQuery.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[popupQuery showInView:self.view];
[popupQuery release];
}
if ( indexPath.row == 1 )
{
UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"Unsell" otherButtonTitles:#"Edit Item", #"Edit Sale",#"Sold",#"Scrap", nil];
popupQuery.tag=5;
popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[popupQuery showInView:self.view];
[popupQuery release];
}
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0)
{
Updateasset *object=[[Updateasset alloc]initWithNibName:#"Updateasset" bundle:nil];
[self presentModalViewController:object animated:NO];
[object release];
}
else if (buttonIndex == 1)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Remove" message:#"Do you want to Remove"
delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
else if (buttonIndex == 2)
{
Egarageselling *object=[[Egarageselling alloc]initWithNibName:#"Egarageselling" bundle:nil];
[self presentModalViewController:object animated:YES];
[object release];
}
else if (buttonIndex == 3)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Scrap" message:#"Do you want to Scrap"
delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
else if (buttonIndex == 4)
{
}
else if (buttonIndex == 5)
{
}
else if (buttonIndex == 6)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Scrap" message:#"Do you want to Scrap"
delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
else if (buttonIndex == 7)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Scrap" message:#"Do you want to Scrap"
delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
else if (buttonIndex == 8)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Scrap" message:#"Do you want to Scrap"
delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
else if (buttonIndex == 9)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Scrap" message:#"Do you want to Scrap"
delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
}
How to solve this problem.
Set different tags for two actionsheets like
popupQuery.tag=5;
popupQuery.tag=6;
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(actionSheet.tag==5)
{
if (buttonIndex == 0)
{
}
so on
}
if(actionSheet.tag==6)
{
if (buttonIndex == 0)
{
}
so on
}
}
I might just add, that in a situation like this I would say that best practice is to make your actionsheet to public or private instance variables. It would look like this in your header file (for public):
#property (nonatomic, retain) UIActionSheet *as1;
#property (nonatomic, retain) UIActionSheet *as2;
In your implementation file you synthesize them, like this:
#synthesize as1, as2;
Then remember to set them from where you now allocate your popupQuery's today:
...
self.as1 = popupQuery;
...
...
self.as2 = popupQuery;
...
And in your delegate method you can now do it like this (which I also find more readable if I should ever present the code for another developer):
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(self.as1 == actionSheet) {
if (buttonIndex == 0) { ... }
...
}
if(self.as2 == actionSheet) {
if (buttonIndex == 0) { ... }
...
}
}
I hope you find it useful in this and many other cases. I myself, do it like that, all the time. No harm in having a public or private reference for that matter to you objects. You never know when they might come in handy.

Is it possible to show alert in `clickedButtonAtIndex` method of alertview

In delegate method ofUIalerview I am trying to show another alert after finishing the process on clicking button index. Is it possible to do so? Also I want to call a method on button click of alert. How can I do that?
I am trying in this way. Is is correct?
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 1)
{
inappPurchaseViewController = [[InAppPurchaseViewController alloc] init];
[inappPurchaseViewController Upgrade:nil];
[inappPurchaseViewController release];
UIAlertView *purchasedone = [[UIAlertView alloc] initWithTitle:#"Enable Ads" message:#"You can Enable Ads from Settings Option" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[purchasedone show];
[purchasedone release];
}
}
You want to use tags for your UIAlertViews
Assigning tags
UIAlertView *firstAlert = [[UIAlertView alloc] initWithTitle:#"Do something first" message:#"This is the first UIAlertView" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[firstAlert setTag: 0];
[firstAlert show];
[firstAlert release];
Handling those tags
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (alertView.tag)
{
case 0: /* firstAlert */
{
if(buttonIndex == 1)
{
inappPurchaseViewController = [[InAppPurchaseViewController alloc] init];
[inappPurchaseViewController Upgrade:nil];
[inappPurchaseViewController release];
UIAlertView *purchaseDone = [[UIAlertView alloc] initWithTitle:#"Enable Ads" message:#"You can Enable Ads from Settings Option" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[purchaseDone setTag: 1];
[purchaseDone show];
[purchaseDone release];
}
}
break;
case 1: /* purchaseDone */
{
/* purchaseDone uialertview was triggered, handle it here. */
}
break;
}
}
UIAlertView *a = [[UIAlertView alloc] initWithTitle:#"a" message:#"b" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"ca",nil];
[a show];
[a release];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 1)
{
UIAlertView *purchasedone = [[UIAlertView alloc] initWithTitle:#"Enable Ads" message:#"You can Enable Ads from Settings Option" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
purchasedone.tag = 55;
[purchasedone show];
[purchasedone release];
}
if([alertView tag] == 55 && buttonIndex == 0)
{
UIAlertView *purchasedone = [[UIAlertView alloc] initWithTitle:#"New Alert" message:#"New Alert Message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[purchasedone show];
[purchasedone release];
}
}
write UIAlertViewDelegate in .h file.
put the following line on top of your .m file
`#define PREVIOUS_ALERT 101`
`#define NEW_ALERT 102`
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
//you need to make a condition here for checking the alert else it will go into infinite loom of showing alert after alert
//you can do it by setting tag to alert something like this
if(alertView.tag == PREVIOUS_ALERT) //sot the previous alert tag to this where you created it
{
if(buttonIndex == 1)
{
inappPurchaseViewController = [[InAppPurchaseViewController alloc] init];
[inappPurchaseViewController Upgrade:nil];
[inappPurchaseViewController release];
UIAlertView *purchasedone = [[UIAlertView alloc] initWithTitle:#"Enable Ads" message:#"You can Enable Ads from Settings Option" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
purchasedone.tag = NEW_ALERT;
[purchasedone show];
[purchasedone release];
}
}
else
{
//code for your second alert
}
}