【发布时间】:2012-02-17 15:10:47
【问题描述】:
我需要加载一个封装在 UINavigationController 中的 UIWebView,并将其显示在模态视图上。 UINavigationController 应该在顶部显示带有“返回”的导航栏(我没有找到返回按钮,所以我使用了“完成”按钮)。我不应该使用笔尖,所以我只能以编程方式进行。代码必须基本上充当可以与任何应用程序集成的库。它必须同时适用于 iPhone 和 iPad。
这是我到目前为止所做的:
我创建了一个 WebViewController 类 -
@interface WebViewController : UIViewController
{
UIWebView *m_cWebView;
}
@property (nonatomic, retain) UIWebView *m_cWebView;
@end
- (void)loadView
{
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
webFrame.size.height -= self.navigationController.navigationBar.frame.size.height;
UIWebView *pWebView = [[UIWebView alloc] initWithFrame:webFrame];
pWebView.autoresizesSubviews = YES;
pWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
self.view = pWebView;
pWebView.scalesPageToFit = YES;
self.m_cWebView = pWebView;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(back:)];
}
- (void)viewDidLoad
{
[super viewDidLoad];
if( m_cWebView != nil )
{
NSURL *url = [NSURL URLWithString:@"http://www.google.co.in"];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[m_cWebView loadRequest:request];
}
}
- (IBAction)back:(id)sender
{
[self dismissModalViewControllerAnimated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
return nil;
}
- (id)init
{
return self;
}
在主视图控制器中
@property (retain, atomic) UINavigationController *navCon;
-(IBAction)buttonPressed:(id)sender
{
if( navCon == nil )
{
WebViewController* webViewController = [[WebViewController alloc] init];
navCon = [[UINavigationController alloc] initWithRootViewController:webViewController];
}
[self presentModalViewController:navCon animated:YES];
}
到目前为止,它运行良好。现在我的问题:
- 我对 iOS 世界完全陌生。上面的代码是正确的还是有问题?
- 代码应该是使用带有 ARC 的 Xcode 4.2 编译的,所以我认为我不需要担心内存问题。
- 计算WebView初始大小的逻辑是否正确(我取主屏大小并减去导航栏高度)?
- 如何处理方向更改,以便在更改方向时,我的 Navigation View 和 WebView 都会调整到新的方向?
谢谢
编辑:
我尝试使用轮换来实现,但我无法让它发挥作用。 willRotateToInterfaceOrientation 没有在我的 WebViewController 上被调用,即使我从 shouldAutorotateToInterfaceOrientation 返回 YES(并且这个方法被调用)。当我尝试使用空笔尖(使用此空笔尖加载 WebViewController)时,它工作正常。
添加第 5 个要求:
- 另一个额外的要求是,当此模态对话框关闭时,呈现此对话框的视图应处于正确的方向(即,当模态对话框启动时,方向事件也会传递给呈现此模态视图的视图)。
谢谢
【问题讨论】:
标签: iphone ipad uiwebview uinavigationcontroller orientation