UIAlertView to UITableViewController - iphone

When I press the 'OK' button at the UIAlertView I wanted it to go back to UITableViewController but when I click it doesn't go back.
QuizViewController.h:
#interface QuizViewController : UIViewController <UIAlertViewDelegate> {
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
#end
QuizViewController.m
-(IBAction)buttonWasClicked:(id)sender{
UIButton *resultebutton= (UIButton*)sender;
if (qCount < totalQuestions) {
id prevQuestion = [appDelegate.qs objectAtIndex:qCount-1];
NSString * correctAns = [prevQuestion labelAns];
if ([correctAns isEqualToString:resultebutton.titleLabel.text])
myScore += 5;
NSLog(#"The button title is %# ", correctAns);
NSLog(#"The button title is %# ", resultebutton.titleLabel.text);
NSString *finishingStatement = [[NSString alloc] initWithFormat:#"Your score so far is %i!", myScore];
theScore.text = finishingStatement;
id nextQuestion = [appDelegate.qs objectAtIndex:qCount];
quizLbl.text = [nextQuestion labelQn];
headerLbl.text = [nextQuestion labelHeader];
[qBtn setTitle:[nextQuestion labelBtn] forState:UIControlStateNormal];
[qBtnB setTitle:[nextQuestion labelBtnB] forState:UIControlStateNormal];
[qBtnC setTitle:[nextQuestion labelBtnC] forState:UIControlStateNormal];
[qBtnD setTitle:[nextQuestion labelBtnD] forState:UIControlStateNormal];
qCount++;
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Results" message:[[NSString alloc] initWithFormat:#"Your total score will be %i!", myScore]
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
QuizTableViewController *quizTable = [self.storyboard instantiateViewControllerWithIdentifier:#"quizTable"];
[self.navigationController presentModalViewController:quizTable animated:YES];
}

You should not declare a delegate method in your .h file like:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
instead give the delegate:self in alertView like this:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Results" message:[[NSString alloc] initWithFormat:#"Your total score will be %i!", myScore]
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
and declare in your .h file like
#interface QuizViewController : UIViewController<UIAlertViewDelegate>
and then use [self.navigationController presentModalViewController:quizTable animated:YES];
in your delegate method at the click of button.index=0.

The reason why you don't get called back is that you set your UIAlertView delegate to nil. It needs to be set to self in order for that object to receive a callback when the alert is dismissed:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Results"
message:[[NSString alloc] initWithFormat:#"Your total score will be %i!", myScore]
delegate:self
cancelButtonTitle:#"OK" otherButtonTitles: nil];

Use this:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
if([alertView.title isEqualToString:#"the_title_of_your_alert"]){
//above line is to identify your alert, if you have several ones
if(buttonIndex == 0)
//do this
else if (buttonIndex == 1)
//do that
else if //bla bla bla, find the button, and if it is your "ok" button,
//go to your TableViewController
}
}
And alertView's delegates should not be declared that way, just implement the delegate methods.

Use this code to go back.
[self.navigationController popViewControllerAnimated:YES];

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 dismiss alertview when click on the button in that alertview

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.

Cannot show UIAlertView with a text field

This piece of code which is supposed to show an alert window with a text input:
self.alert = [[UIAlertView alloc] initWithTitle:#"Hello" message:#"How are you?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
self.alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[self.alert show];
Causes this error:
Thread 7: Program received signal: "EXC_BAD_ACCESS"
This is how self.alert is defined:
#interface MyClass : NSObject
{
UIAlertView *alert;
id <MyClassDelegate> __unsafe_unretained delegate;
}
#property (nonatomic, retain) UIAlertView *alert;
#property (unsafe_unretained) id <MyClassDelegate> delegate;
The problem is maybe because of the customize.
I do not know why, but appear to me that the problem is because of the use of threads + customize of your alert.
Can you try to show this alert on the main thread? What happen?
You probably get an error in this line:
self.alert.alertViewStyle = UIAlertViewStylePlainTextInput;
What you need to do if yes, is perform this in the main thread.
- (void) yourMethod{
[self performSelectorOnMainThread:#selector(yourMethod2) withObject:nil waitUntilDone:NO];
}
- (void) yourMethod2{
self.alert = [[UIAlertView alloc] initWithTitle:#"Hello" message:#"How are you?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
self.alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[self.alert show];
}
Sorry to can't help you more than that, but I do not know exactly what happen, but I already read about issues when editing things to show, in other threads.
Hope it help you!
The EXC_BAD_ACCESS is caused by accessing a released object. To avoid this make your call to UIAlertView kind of modal:
Function body:
-(void)checkSaving
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Do you want to add these results to your database?"
message:#"\n\n"
delegate:self
cancelButtonTitle:#"No"
otherButtonTitles:#"Save", nil];
alert.alertViewStyle = UIAlertViewStyleDefault;
[alert show];
//this prevent the ARC to clean up :
NSRunLoop *rl = [NSRunLoop currentRunLoop];
NSDate *d;
d= (NSDate*)[d init];
while ([alert isVisible])
{
[rl runUntilDate:d];
}
}
Your choice result:
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 1)//Save
{
//do something
}
if (buttonIndex == 0)//NO
{
//do something
}
}
Register the functions in the interface declaration:
#interface yourViewController ()
-(void)checkSaving
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
//...
#end
To call:
[self checkSaving];
I wish this will help you.

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.

UIAlertView not responding to UIAlertViewDelegate

I'm using logos for iPhone (MobileSubstrate addons), with a .h file for my
#interface MyClass : NSObject <UIAlertViewDelegate>
and the
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 0) {
is in the .m, but nothing is working, when tapping the buttons on the alert, it doesn't invoke what I have set for each buttonIndex.
Thanks.
Edit: Here's what I've got;
#import "Tweak.h"
%hook ASApplicationPageHeaderView
- (void)_showPurchaseConfirmation {
UIAlertView *alert = [[UIAlertView alloc] init];
[alert setTitle:#"title"];
[alert setMessage:#"message"];
[alert setDelegate:self];
[alert addButtonWithTitle:#"button 1"];
[alert addButtonWithTitle:#"continue"];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) { //also tried (UIAlertView *)alertView
UIAlertView *lol = [[UIAlertView alloc] init];
[lol setTitle:#"button 1"];
[lol setMessage:#"button 1"];
[lol setDelegate:self];
[lol addButtonWithTitle:#"lol"];
[lol show];
[lol release];
} else {
%orig;
}
}
%end
You'll most likely need to register your class as the delegate at some point using something along the lines of:
[yourAlertViewObject setDelegate:self];
As the UIAlertViewDelegate Protocol Reference docs say (emphasis mine):
If you add your own buttons or
customize the behavior of an alert
view, implement a delegate conforming
to this protocol to handle the
corresponding delegate messages. Use
the delegate property of an alert view
to specify one of your application
objects as the delegate.
Define your alert within that class and declare the alert delegate to self hope it start working to you
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert View "
"
message:#"Would you like to do something?"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Button1", #"Button2", nil];
[alert show];
[alert release];
You just need to put %new in front of the alertView delegate:
%new
-(void) alertView:...