Resolved Direct User to a new view with alert view

Discussion in 'iPhone/iPad Programming' started by webznz, Apr 18, 2011.

  1. webznz, Apr 18, 2011
    Last edited: Apr 19, 2011

    macrumors member

    Joined:
    Mar 8, 2011
    Messages:
    82
    Location:
    Hobbitin
    #1
    I am currently setting up a little security feature on this app I'm making,
    whats happening as soon as my app is launched it sends its cookie to the database in return for a new cookie. If the user starts the app for the first time, or has a cookie that is out of sync because of various reasons (for instance they have given heir registration code to someone else to use) they will be given an Alert View error msg...

    Code:
    		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please Register Device"
    														message:@"click OK to register"
    													   delegate:nil
    											  cancelButtonTitle:@"OK"
    											  otherButtonTitles:nil];
    		[alert autorelease];
    		[alert show];
    I am wondering how I would go about sending the user to a new view (that will allow them to enter their registration code) with the
    Code:
    cancelButtonTitle
    button when it is pressed on the alert view
     
  2. macrumors 6502

    Joined:
    Jan 13, 2011
    Messages:
    350
    Location:
    Los Angeles, CA
    #2
    I believe you have to use the UIAlertViewDelegate method(s).
     
  3. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Messages:
    1,343
    Location:
    Aartselaar // Antwerp // Belgium
    #3
    Code:
    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    	NSString *buttonTitle=[actionSheet buttonTitleAtIndex:buttonIndex];
    	if ([buttonTitle isEqualToString:@"Start driving"]) {
    		success = [DriverService moveToNextState];
    	} else if ([buttonTitle isEqualToString:@"Start job"]) {
    		success = [DriverService moveToNextState];
    	} else if ([buttonTitle isEqualToString:@"Finish job"]){
    		success = [DriverService moveToNextState];
    		if (success){
    			[self.navigationController popViewControllerAnimated:YES];
    			return;
    		}
    	}
    	if (success){
    		self.driver = success;
    		[self updateMap];
    	}
    	
    }
    
    something like this. but then with the alertView instead of actionsheet ;)
     
  4. webznz, Apr 19, 2011
    Last edited by a moderator: Apr 19, 2011

    macrumors member

    Joined:
    Mar 8, 2011
    Messages:
    82
    Location:
    Hobbitin
    #4
    cheers dude :) will try it out now.

    EDIT:

    Im not sure if im using this correctly or not. I figure if my cancelButtonTitle:mad:"OK" is pressed it should initiate my alertView:didDismissWithButtonIndex: method... is that right? sorry just having abit of a moment with this.

    Code:
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    	//we are just checking to make sure we are getting the SML
    	NSString *cookieCheck = [[[NSString alloc] initWithData:cookieData encoding:NSUTF8StringEncoding] autorelease];
    	NSLog(@"yum yum feed me more cookie!! %@",cookieCheck);
    	
    	//Put cookie check stuff here.. 
    	
    	//CODE=1
    	if([cookieCheck rangeOfString:@"Code=1"].location != NSNotFound) {
    		//found it Send error prompt (OK loads new view for fucnt Reg) 
    		NSLog(@"cookieCode = code1");
    		
    		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please Register Device"
    														message:@"click OK to register"
    													   delegate:nil
    											  cancelButtonTitle:@"OK"
    											  otherButtonTitles:nil];
    		
    		[alert autorelease];
    		[alert show];
    		
    	}else {
    		NSLog(@"cookieCode = code?");
    	}
    
    }
    
    - (void)alertView:(UIAlertView *)alert didDismissWithButtonIndex:(NSInteger)buttonIndex {
    	//Test
            NSLog(@"msg from alertView method");
        
    
    	
    }
    
     
  5. macrumors member

    Joined:
    Mar 8, 2011
    Messages:
    82
    Location:
    Hobbitin
    #5
    it just clicked... made me mad as hell that i didnt think of this right away.... set delegate of the alertView to self..

    Solution.
    Code:
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    	//..................
    	//CODE=1
    	if([cookieCheck rangeOfString:@"Code=1"].location != NSNotFound) {
    		//found it Send error prompt (OK loads new view for fucnt Reg) 
    		NSLog(@"cookieCode = code1");
    		
    		
    		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please Register Device"
    														message:@"click OK to register"
    													   delegate:[B][COLOR="Red"]self[/COLOR][/B]
    											  cancelButtonTitle:@"OK"
    											  otherButtonTitles:nil];
    		
    		// If i want to add text field to alert use this code....
    		
    		UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
    		[myTextField setBackgroundColor:[UIColor whiteColor]];
    		[alert addSubview:myTextField];
    		
    		[alert autorelease];
    		[alert show];
    		
    	}else {
    		//cant find appropriate response...
    		NSLog(@"cookieCode = code?");
    	}
    
    }
    
    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    	
            NSLog(@"msg from alertView method");
        
    
    	
    }
     
  6. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Messages:
    1,343
    Location:
    Aartselaar // Antwerp // Belgium
    #6
    But now the log is triggering on all the buttons or not?!
    U need to create a buttonindex, or like in my example, compare the titles.
    Greetings.
     
  7. macrumors member

    Joined:
    Mar 8, 2011
    Messages:
    82
    Location:
    Hobbitin
    #7
    Yea, I didn't do a button index because I only have the one button option. :eek:
     

Share This Page