在UIWebView中打开一个URL

时间:2015-01-07 08:46:17

标签: ios objective-c iphone url home-button

我有一个重要的问题。我正在创建一个iPhone应用程序。 我有一个带有goBack,goForward和reload的UIWebView(https://www.google.de/)。 现在,我想要一个按钮,例如条形按钮项目名为Home或Homepage,用于在此WebView中打开URL(https://www.google.de/),以返回主页。 我怎样才能做到这一点?我怎么能说"用于在WebView中打开链接的按钮?我要求回答。

//  ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (nonatomic, strong) IBOutlet UIWebView *site   ;
@end



//  ViewController.m

#import "ViewController.h"

@interface ViewController ()
@end
@implementation ViewController
-(IBAction)refresh:(id)sender; {

    NSURL *url=[NSURL URLWithString: @"https://www.google.de/"]; NSURLRequest * requestURL=[NSURLRequest requestWithURL:url]; [_site loadRequest:requestURL];

}
- (void)viewDidLoad {

    [self refresh:self];

    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

}
- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}
@end

1 个答案:

答案 0 :(得分:2)

UIWebView有方法goBackgoForward。您可以进行操作并将其与条形按钮绑定,以便随时返回。

- (IBAction)goBack:(id)sender {
    [_site goBack];
}

- (IBAction)goForward:(id)sender {
    [_site goForward];
}

- (IBAction)goHomepage:(id)sender {
    NSURL *url=[NSURL URLWithString: @"https://www.google.de/"]; 
    NSURLRequest *requestURL=[NSURLRequest requestWithURL:url]; 
    [_site loadRequest:requestURL];
}

为了向您提供更多帮助,here's a tutorial on this,如果您不想阅读该教程,可以查看他们的source code from here