Call html pages located different folder via UIbutton objective-c

半腔热情 提交于 2020-01-26 00:39:37

问题


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];
}


来源:https://stackoverflow.com/questions/12917561/call-html-pages-located-different-folder-via-uibutton-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!