Call html pages located different folder via UIbut

2019-09-05 04:26发布

问题:

I have one page with 4 buttons, it's UIViewController, and in my application I have 4 folder that inside of each folder I have one html file , I have another webViewController that I should load this html files inside of that webViewController, I mean when click on first button load first html , if click second load second html and .... would you please help me to implement this:

here is my method :

-(void)loadWebView{

NSURL *url = [NSURL fileURLWithPath:[ [ NSBundle mainBundle ] pathForResource:  
@"TestName/TestName1/Test1Name1" ofType:@"html" ]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}

How can I put address like this to my method

@"TestName/TestName1/Test1Name1" 

 I mean @"TestName%d/TestName%d/Test%dName" ofType:@"html" 

and set action to my button to when click on first button goes to first Folder 1 and load html when click on second button goes to second Folder 1 and load html and so on ...

here is my buttons

- (IBAction)ActionButton1:(id)sender {
NSLog(@"A1");
[self.webViewController loadWebView];
}

- (IBAction)ActionButton2:(id)sender {
NSLog(@"A1");
[self.webViewController loadWebView];
}

. . . .

Thanks in advance!

Edit:

File structure folder

TestName1

     TestName1

        TestName1.html

Second

TestName2

     TestName2

        TestName2.html

回答1:

Just add a parameter to your loadWebView method (an int) and create the file path accordingly...

Something like

-(void)loadWebView:(int)htmlNum{

    NSString *htmlPath = [NSString stringWithFormat:@"TestName/TestName%d/Test%dName%d",htmlNum,htmlNum,htmlNum];
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:htmlPath ofType:@"html"]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
}

You then create a method for your buttons that calls it properly (assigning a tag to your buttons for example could really simplify your code : one method for all buttons...)

- (IBAction)ActionButton:(UIButton*)sender {
    [self.webViewController loadWebView:sender.tag];
}