Pages

Sunday, July 28, 2013

UIAlertView in xCode

Here i am showing you an Alert as in below Image


It has 2 buttons OK and Cancel. We can put any name in place of "OK" or "Cancel".


Ok... So here is the simple code for this.


1. First define a UIAlertView in top like




@property (nonatomic) UIAlertView *alert;

2. Add Alert like below 

self.alert = [[UIAlertView alloc]initWithTitle:@"Signup Alert"
              
                                       message:@"We will redirect to our site. where you can signup through your details. Press Ok to switch there or Canel to skip signup."
                                      delegate:nil
                             cancelButtonTitle:@"OK"
                             otherButtonTitles:@"Cancel", nil];
self.alert.delegate = self;
self.alert.tag=101;//add tag to alert

[self.alert show];

3. Now if you have declared delegate to self then write the below function, which will be called automatically.


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(self.alert.tag ==101)     // check alert by tag
    {
        if (buttonIndex == 0)
        {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://myURL.com/signupPage.aspx"]];
        }
        else
        {
            //Skipping Signup
        }
    }
}