Wednesday, 18 July 2012

UIAlertView and UIActionSheet in XCode


.h File

@interface ViewController : UIViewController <UIAlertViewDelegate> 
{
}
@end       


.m File:
UIAlertView *alert=[[UIAlertView alloc]
                            initWithTitle:@"Hello World!"
                            message:@"This is your first UIAlertview message"
                            delegate:self
                            cancelButtonTitle:@"OK"
                            otherButtonTitles:nil];        //otherButtonTitles:@”Next Page”,@”Last Page”];
[alert addButtonWithTitle:@"Next Page"];
  [alert addButtonWithTitle:@"Last Page"];

       [alert show];
        [alert release];
   


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"OK"])
{
NSLog(@"Next Page was selected.");
[self dismissModalViewControllerAnimated:YES];
}

        else if([title isEqualToString:@"Next Page"])
{
//Codings
NSLog(@"Next Page was selected.");
}

else if([title isEqualToString:@"Next Page"])
{
//Codings
NSLog(@"Last Page was selected.");
}
}



     UIActionSheet

.h File:
@interface ViewController : UIViewController <UIActionSheetDelegate>
{
  UIActionSheet *sheet;
}
@end
.m File:
 UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Share" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Facebook", @"Twitter", nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
    [actionSheet showInView:self.view];
    [actionSheet release];


-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{
    if(buttonIndex == 0) 
   {
        //An Action 
   } 
   else if(buttonIndex == 1) 
  {
        //Another Action
  }
}


No comments: