分享

不使用IB在UIWebView里面扩展添加UIMenuItem菜单

 jerry_tom123 2016-01-13

创建一个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;
}
复制代码

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多