不使用IB在UIWebView里面扩展添加UIMenuItem菜单
创建一个controller名为YKWebViewDemoViewController
.h文件代码如下
#import <UIKit/UIKit.h> @interface YKWebViewDemoViewController : UIViewController<UIWebViewDelegate>{ } @property (strong, nonatomic) UIWebView *webView; - (void)loadWebPageWithString:(NSString*)urlString; - (void)customAction1:(id)sender; - (void)customAction2:(id)sender; @end
.m文件代码
#import "YKWebViewDemoViewController.h" @interface YKWebViewDemoViewController () @end @implementation YKWebViewDemoViewController @synthesize webView; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. CGRect rect = [[UIScreen mainScreen] bounds]; webView = [[UIWebView alloc]initWithFrame:rect]; webView.scalesPageToFit =YES; webView.delegate =self; [self.view addSubview:webView]; //加载到自己的view NSString *urlAddress = @"http://www.baidu.com"; [self loadWebPageWithString:urlAddress]; } - (void)viewDidUnload { [super viewDidUnload]; webView = nil; // Release any retained subviews of the main view. } - (void)dealloc { [webView release]; [super dealloc]; } //自定义弹出菜单 - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; UIMenuItem *customMenuItem1 = [[[UIMenuItem alloc] initWithTitle:@"custom 1" action:@selector(customAction1:)] autorelease]; UIMenuItem *customMenuItem2 = [[[UIMenuItem alloc] initWithTitle:@"custom 2" action:@selector(customAction2:)] autorelease]; UIMenuController *menu = [UIMenuController sharedMenuController]; [menu setMenuItems:[NSArray arrayWithObjects:customMenuItem1, customMenuItem2, nil]]; [menu setMenuVisible:YES animated:YES]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [[UIMenuController sharedMenuController] setMenuItems:nil]; } -(void)customAction1:(id)sender { NSLog(@"customAction1 is active"); } -(void)customAction2:(id)sender { NSLog(@"customAction2 is active"); } - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { [super canPerformAction:action withSender:sender]; if (action == @selector(customAction1:) || action == @selector(customAction2:)) { return YES; } else { return NO; } } -(BOOL)canBecomeFirstResponder { [super canBecomeFirstResponder]; return YES; } //加载网址 - (void)loadWebPageWithString:(NSString*)urlString { NSURL *url =[NSURL URLWithString:urlString]; NSURLRequest *request =[NSURLRequest requestWithURL:url]; [webView loadRequest:request]; } - (void)webViewDidStartLoad:(UIWebView *)webView; { NSLog(@"didStartLoad is called"); } - (void)webViewDidFinishLoad:(UIWebView *)webView { NSLog(@"didFinishLoad is called"); } - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { UIAlertView *alterview = [[UIAlertView alloc] initWithTitle:@"" // message:[error localizedDescription] message:@"页面加载失败。" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; [alterview show]; [alterview release]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } @end
在AppDelegate.h里面添加如下代码
#import <UIKit/UIKit.h> @class YKWebViewDemoViewController; @interface YKAppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) YKWebViewDemoViewController *viewController; @end
在AppDelegate.m里面导入Controller头文件
#import "YKWebViewDemoViewController.h"
并且添加
@synthesize viewController = _viewController;
修改原有didFinishLaunchingWithOptions代码如下
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. self.viewController = [[YKWebViewDemoViewController alloc] init]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; return YES; }