不使用IB在UIWebView里面扩展添加UIMenuItem菜单
2012-04-11 18:45
232 查看
创建一个controller名为YKWebViewDemoViewController
.h文件代码如下
.m文件代码
在AppDelegate.h里面添加如下代码
在AppDelegate.m里面导入Controller头文件
并且添加
修改原有didFinishLaunchingWithOptions代码如下
.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; }
相关文章推荐
- 使用libzip读取修改zip文件comment的方法(支持添加扩展字符)
- JUnit在intellij idea中只能在test里面才能使用,否则不能添加import
- 使用扩展方法添加到String类中简化脚本注入处理
- iOS开发:给项目工程里面添加Podfile配置文件,以及CocoaPods的简单使用
- windows下安装redis服务 AND PHP中添加扩展 AND PHP中使用
- 在uboot里面添加环境变量使用run来执行
- 使用分类向某个现有类添加方法(类扩展,范畴)
- 牛腩购物18 : 添加商品页面2 。泛型List<T>的使用,linq 的使用, 如何在 DropDownList 里面绑定二级分类(运用linq),fileupload和图片控件的配合使用用来上传图片,try catch
- php5.2下添加php_mongo.dll扩展无法使用的原因及解决办法
- subline text 3 里面添加 SASS 的扩展
- 在IB中使用Auto Layout来添加UIScrollView
- Linux rpm 命令参数使用详解:RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的“添加/删除程序”
- 使用 Chrome 扩展将 YouTube 播放器控件添加到 Linux 桌面
- 使用扩展方法,向现有类型“添加”方法
- 在Solr4.9中使用IKAnalyzer,实现同义词,扩展词库,停顿词的添加
- ruby应用里添加zepto.min.js但是无法使用里面的方法
- macOS 中使用 phpize 动态添加 PHP 扩展的错误解决方法
- 数组的创建/查找数组里面的内容/添加数组中元素/使用指定的字符串把数组链接起来/判断数组内是否有指定的数组元素/四种遍历进行输出数组中的元素有哪些
- 在uboot里面添加环境变量使用run来执行并启动时自动执行run
- ruby应用里添加zepto.min.js但是无法使用里面的方法