Two UiAlertView messages with different button actions - objective-c

I have been successfully created two alerts with four buttons. The first AlertView works great but the second alertView crashes my up. Here is my code:
-(void)showAlertWithTextField{
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"info" message:#"Set Time For The Game." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"cancel", nil];
[alertView setTag:101];
alertView.alertViewStyle=UIAlertViewStylePlainTextInput;
[[alertView textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
[[alertView textFieldAtIndex:0] becomeFirstResponder];
[alertView show];
}
-(void)showexitAlertView
{
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"Timeout" message:[NSString stringWithFormat:#"Your final score is: %i", runningSore] delegate:self cancelButtonTitle:#"Restart" otherButtonTitles:#"Exit", nil];
[alertView setTag:102];
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UITextField *field = [alertView textFieldAtIndex:0];
setTime = [field.text intValue];
NSLog(#"%d", setTime);
if (alertView.tag == 101 && buttonIndex == 0) {
//if (buttonIndex == 0){
NSLog(#"Ok");
[self setTimer];
[self countDownTimerPrepare];
[self countDownTimerIntro];
[self prepareForIntroAnimation];
[self performIntroAnimation];
[self categoriesList];
}
else if (buttonIndex == 1){
NSLog(#"Cancel");
ChooseViewController * controller = [[ChooseViewController alloc] initWithNibName:#"ChooseViewController" bundle:nil];
//controller.countdownLabel.text= score;
[self presentViewController:controller animated:YES completion:nil];
}
else if (alertView.tag==102 && buttonIndex == 0){
NSLog(#"Restart");
}
else if (buttonIndex == 1){
NSLog(#"Exit");
}
}
Here is the drbugging message when i press a button from the second Alert view:
2013-09-11 17:12:06.106 Diplomatiki[2808:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'textFieldIndex (0) is outside of the bounds of the array of text fields'
* First throw call stack:
(0x1c42012 0x1737e7e 0x1c41deb 0xa7f0f0 0xb5d0 0xa72020 0x174b705 0x682920 0x6828b8 0x743671 0x743bcf 0x742d38 0x6b233f 0x6b2552 0x6903aa 0x681cf8 0x1fc2df9 0x1fc2ad0 0x1bb7bf5 0x1bb7962 0x1be8bb6 0x1be7f44 0x1be7e1b 0x1fc17e3 0x1fc1668 0x67f65c 0x29fd 0x2925 0x1)
libc++abi.dylib: terminate called throwing an exception
I will apreciate any suggestions. Thank you in advance.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UITextField *field = [alertView textFieldAtIndex:0];
setTime = [field.text intValue];
This is error, your second alertView doesn't contain any textField. As in documentation says:
Retrieve a text field at an index - raises NSRangeException when textFieldIndex is out-of-bounds.
The field at index 0 will be the first text field (the single field or the login field), the field at index 1 will be the password field.
This is a solution that should work
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 101 && buttonIndex == 0) {
UITextField *field = [alertView textFieldAtIndex:0];
setTime = [field.text intValue];
NSLog(#"%d", setTime);
...
}

Crash is due to your second alert view don't have a textfield. But you are trying to access the textfield and it's value from the clickedButtonAtIndex:regardless which alerview is clicked.
Implement like: clickedButtonAtIndex
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 101)
{
UITextField *field = [alertView textFieldAtIndex:0];
setTime = [field.text intValue];
NSLog(#"%d", setTime);
if( buttonIndex == 0)
{
NSLog(#"Ok");
[self setTimer];
[self countDownTimerPrepare];
[self countDownTimerIntro];
[self prepareForIntroAnimation];
[self performIntroAnimation];
[self categoriesList];
}
else if (buttonIndex == 1)
{
NSLog(#"Cancel");
ChooseViewController * controller = [[ChooseViewController alloc] initWithNibName:#"ChooseViewController" bundle:nil];
[self presentViewController:controller animated:YES completion:nil];
}
}
else if (alertView.tag==102)
{
if(buttonIndex == 0)
{
NSLog(#"Restart");
}
else if (buttonIndex == 1)
{
NSLog(#"Exit");
}
}
}

Axel, the same problem as in previous case
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
if (alertView.tag == 101){
NSString *inputText = [[alertView textFieldAtIndex:0] text];
if( [inputText length] >= 1 ){
return YES;
}
else{
return NO;
}
}else{
return NO;
}
}

Related

UIAlertView buttonIndex not working

I guess I'm missing something obvious. I have UIAlertView to get app review but I can't get the buttons to do anything. I've called the UIAlertViewDelegate in my.h, I also have anther UIAlertview which is just on an IBAction btn and that works fine although it just had cancel btn.
I tried giving alert.tag = 1 but that never made any difference so I commented out my first UIAlertview, so I just have one alert, still no joy. I guess I am missing something simple.
I've also tried alertview.cancelButtonIndex or alertview.firstOtherButtonIndex instead of 0
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSLog(#"index 0 ");
}
else if (buttonIndex == 1) {
NSLog(#"index 1 ");
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"www.google.com"]];
NSUserDefaults *rateApp = [NSUserDefaults standardUserDefaults];
NSInteger appLaunch = [ rateApp integerForKey:#"appLaunch"];
appLaunch = 0 ;
[rateApp setInteger: appLaunch forKey:#"appLaunch"];
}
else if (buttonIndex == 2) {
NSLog(#"index 2 ");
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
//rate app appLaunch == 5 || appLaunch ==10
NSUserDefaults *rateApp = [NSUserDefaults standardUserDefaults];
NSInteger appLaunch = [ rateApp integerForKey:#"appLaunch"];
if (appLaunch == 1 ) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Like this app ?" message:#"Why not rate at the app store" delegate:nil cancelButtonTitle:#"No thanks" otherButtonTitles:#"Yes",#"Remind me later", nil];
// alert.tag = 1;
[alert show];
}
Thanks for any help.
AlertView's delegate should be self instead of nil, if you want to invoke its delegate method.
Use this Code :
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Like this app ?"
message:#"Why not rate at the app store"
delegate:self
cancelButtonTitle:#"No thanks"
otherButtonTitles:#"Yes",#"Remind me later", nil];

uiscrollview rest when touch

i have UIScrollView it has next button when click it run this code
-(IBAction)ScrollPage:(id)sender {
NSLog(#"ScrollPage Called");
NSInteger positionX;
if ([sender tag] == 1) {
positionX = AddNewScroll.contentOffset.x - 320;
}else {
positionX = AddNewScroll.contentOffset.x + 320;
}
[AddNewScroll setContentOffset:CGPointMake(positionX,0) animated:YES];
}
and it will move the scroll to new page , then i have button there when click it will call
-(IBAction)SelectImg:(id)sender {
UIActionSheet *imgMenu = [[UIActionSheet alloc] initWithTitle:#"choose image"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Camera"
otherButtonTitles:#"Album",nil];
imgMenu.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[imgMenu showInView:[UIApplication sharedApplication].keyWindow];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
imgPicker = [[UIImagePickerController alloc] init];
imgPicker.allowsEditing = YES;
imgPicker.delegate = self;
#try {
if (buttonIndex == 0) {
imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:imgPicker animated:YES];
}
if (buttonIndex == 1) {
imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//Start iPhone PhotoLibrary
if ([[UIDevice currentDevice]userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
[self presentModalViewController:imgPicker animated:YES];
} else
//Start iPad PhotoLibrary
if ([[UIDevice currentDevice]userInterfaceIdiom] == UIUserInterfaceIdiomPad){
aPopover = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
[aPopover presentPopoverFromRect:CGRectMake(250, 40, 300, 300) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
}
the Problem is when it call the actionSheet the UIScrollView by itself go to first page contentOffset to 0 then call scrollViewDidScroll
ps. Paging Disable
In the method clickedButtonAtIndex, you are re-allocating your UIImagePickerController. Isn't that the reason why it goes back to 0?
Also, what relation is there between AddNewScroll and imgPicker?

How to check the return value of UIAlertView?

I want to check if the user click "yes" for example I want to put a certain action.
This is my line of code:
UIAlertView* mes=[[UIAlertView alloc] initWithTitle:#"Are you sure?" message:#"this will start a new game" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[mes show];
So I want to say if user tap "yes" preform this action
this is my method: I want to say if the user click "yes" create the new game.
- (IBAction)newGame:(UIButton *)sender {
UIAlertView* mes=[[UIAlertView alloc] initWithTitle:#"Are you sure?" message:#"this will start a new game" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[mes show];
self.flipsCount = 0;
self.game = nil;
for (UIButton *button in self.cardButtons) {
Card *card = [self.game cardAtIndex:[self.cardButtons indexOfObject:button]];
card.unplayble = NO;
card.faceUp = NO;
button.alpha = 1;
}
self.notificationLabel.text = nil;
[self updateUI];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != [alertView cancelButtonIndex])
{
//User clicked ok
NSLog(#"ok");
[self newGame:[UIButton alloc] ];
}
else
{
//User clicked cancel
NSLog(#"cancel");
}
}
it takes ButtonIndex values from left to right .
Insert the following method in the same class (or in the class you have set as delegate):
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
//User clicked ok
}
else
{
//User clicked cancel
}
}
Use the delegate method of UIAlertView as
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)//Yes
{
self.flipsCount = 0;
self.game = nil;
for (UIButton *button in self.cardButtons)
{
Card *card = [self.game cardAtIndex:[self.cardButtons indexOfObject:button]];
card.unplayble = NO;
card.faceUp = NO;
button.alpha = 1;
}
self.notificationLabel.text = nil;
[self updateUI];
}
else//No
{
//do something
}
}

UIAlertView EXC_BAD_ACCESS error

I cannot figure out why I keep getting an EXC_BAD_ACCESS error on the line below in the action sheet method:
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:numberFinal]];
I added my code, but I just don't see why its being over released.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
number = [[results objectAtIndex:indexPath.row]objectForKey:#"phone"];
number = [number stringByReplacingOccurrencesOfString:#"-" withString:#""];
numberFinal = [NSString stringWithFormat:#"tel:%#",number];
//tel:1234567890
NSLog(#"NUMBER:%#",numberFinal);
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UIAlertView *alert2 = [[[UIAlertView alloc]initWithTitle:#"Call" message:#"Call This Person?" delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"YES", nil]autorelease];
alert2.tag = kAlertViewTwo;
[alert2 show];
// [alert2 release];
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the OK/Cancel buttons
if(actionSheet.tag == kAlertViewOne) {
if (buttonIndex == 0)
{
}else{
}
}
else if(actionSheet.tag == kAlertViewTwo) {
if (buttonIndex == 0)
{
//ok button clicked - close alert
}
else
{
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:numberFinal]];
}
}
}
Try do an NSLog before the line and print numberFinal to see if it allocated ok.
Make sure your class is a UIAlertView Delegate
Try the same line of code in another function to see if it causes the same issue. It might be because you are accessing sharedApplication from within the UIAlertView click event.
Hope that helps :)

How to use a UIAlert to open a NIB or UIView

Here is my code so far I can get a URL to work but I want to load a nib from this Alert tab
- (IBAction)aboutAction { // The action called when the about button is clicked.
UIAlertView * aboutView = [[UIAlertView alloc] initWithTitle:#"Alert:" // Create a new UIAlertView named aboutScreen, and allocate it. Set the title to "About"
message:#"MESSAGE GOES HERE"
delegate:self
cancelButtonTitle:#"Got It Thanks!"
otherButtonTitles:#"Donate Now", nil];
[aboutView show]; // Show the UIAlertView on the screen.
[aboutView release]; // Release the UIAlertView from the memory.
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 1) { //Not sure what to do here.
}
}
-(void) alertView:(UIAlertView *) alertview clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 1: {
Accidenthelpercall *help = [[Accidenthelpercall alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:help animated:NO];
}
case 2: {
Accidentdamagecar *damagecar = [[Accidentdamagecar alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:damagecar animated:NO];
}
}
-(void) alertView:(UIAlertView *) alertview clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 1: {
Accidenthelpercall *help = [[Accidenthelpercall alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:help animated:NO];
[help release];
}
case 2: {
Accidentdamagecar *damagecar = [[Accidentdamagecar alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:damagecar animated:NO];
[damagecar release];
}
}