Multiple UIAlertView in same UIViewController: iPhone | Coding Cluster - using asp.net, c#, mvc 4, iphone, php, ios, javascript, in asp.net mvc 3 & more
 

Multiple UIAlertView in same UIViewController: iPhone

Friday

iphone - different alert views in one viewcontroller:
                      The following code is used to display two different alerts with different backgrounds in the same screen, put the following code snippet into your .m file;


Step 1: Define the key tokens.
#define AlertOne 1
#define AlertTwo 2
Step 2: Declare your alert views where ever needed like this. Don't forget to add tag.
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"AlertView Title"  message:@"Message one" delegate:self  cancelButtonTitle:@"Cancel" 
otherButtonTitles:@"OK1", nil]; 
alert1.tag = AlertOne; // assigning the tag for this alert 
[alert1 show]; 
[alert1 release]; 

UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"AlertView Title"  message:@"Message two" delegate:self  cancelButtonTitle:@"Cancel" 
otherButtonTitles:@"OK2", nil]; 
alert2.tag = AlertTwo; // assigning the tag for this alert 
[alert2 show]; 
[alert2 release];
Step 3: Then implement the UIAlertViewDelegate methods like below: 
- (void)willPresentAlertView:(UIAlertView *)alertView { 
UIImage *alertImage = [[UIImage alloc] init]; 
if(alertView.tag == AlertOne) { 
alertImage = [UIImage imageNamed:@"orange.png"]; 
} 
else if(alertView.tag == AlertTwo) { 
alertImage = [UIImage imageNamed:@"blue.png"]; 
} 

alertImage = [alertImage stretchableImageWithLeftCapWidth:16 topCapHeight:14]; 
CGSize theSize = [alertView frame].size; 
UIGraphicsBeginImageContext(theSize); 
[alertImage drawInRect:CGRectMake(0, 0, theSize.width, theSize.height)]; 
alertImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
[[alertView layer] setContents:[alertImage CGImage]]; 
} 
Step 4: Atlast, implement different viewControllers for multiple buttons of UIAlertView like:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
if(alertView.tag == AlertOne) { 
if(buttonIndex == 0) { 
// your code here
  } 
} 
else if(alertView.tag == AlertTwo) { 
if(buttonIndex == 1) { 
// your code here
} else NSLog(@"Hai codingcluster"); }
Please share this, if you think this is useful to others. Thanks!.

0 comments:

Post a Comment

Share your thoughts here...

 
 
 

RECENT POSTS

Boost

 
Blogger Widgets