Thursday, 1 October 2015

How To Work With UIActionsheet Using Diffrent ButtonIndex In Ios/Iphone

==> Here Use Some UIActionSheet Confrim Delegates file And Use it.
=> Take One Object Of It.
=> Create One VIewCOntrollwr Of Use UIACtionSheet And Its Delegate FIle AMthods:

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.webView.delegate = self;
    self.title = @"Apple";
    [self loadRequestFromString:@"http://www.apple.com"];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

==>Declared and Used Implemented UIAction Of Button And It Pass Sender Paremater:

- (IBAction)btnMoreClick:(id)sender{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose your site" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Google" otherButtonTitles:@"Yahoo",@"Facebook",@"Twitter",@"HTML File",nil];
   
    actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
   
    [actionSheet showInView:self.view];

}

=> This Method Used For Load Request URL From Some String Link and Load in UIWebView:

- (void)loadRequestFromString:(NSString*)urlString
{
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:urlRequest];
}
=> Declared Some UI  updated Button Whre Use Using Go FOrwrd , Go backword Reload , Refresh

#pragma mark - Updating the UI
- (void)updateButtons
{
//    NSLog(@"%s loading = %@", __PRETTY_FUNCTION__, self.webView.loading ? @"YES" : @"NO");
    self.forward.enabled = self.webView.canGoForward;
    self.back.enabled = self.webView.canGoBack;
    self.stop.enabled = self.webView.loading;
}
==> Here Implmemted Some Delgate Method And Action Of UIwebView :

#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [self updateButtons];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [self updateButtons];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [self updateButtons];
}
=> Now Here Implemented Some Delegate UIActionSheet:

#pragma mark - UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSLog(@"%d",buttonIndex);
    if (buttonIndex==0) {
       [self loadRequestFromString:@"http://www.google.com"];
        self.title = @"Google";
    }else if (buttonIndex==1){
        [self loadRequestFromString:@"http://www.yahoo.com"];
        self.title = @"Yahoo";
    }else if (buttonIndex==2){
        [self loadRequestFromString:@"http://www.facebook.com"];
        self.title = @"Facebook";
    }else if (buttonIndex==3){
        [self loadRequestFromString:@"http://www.twitter.com"];
        self.title = @"Twitter";
    }else if (buttonIndex==4){
        self.title = @"HTML File";
//        NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"termsCond" ofType:@"htm"];
//        NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
//        [self.webView loadHTMLString:htmlString baseURL:nil];
        NSString *path = [[NSBundle mainBundle] pathForResource:@"termsCond" ofType:@"html"];
        NSURL *url = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        NSURLRequest *req = [NSURLRequest requestWithURL:url];
        [self.webView loadRequest:req];
    }
   
}