【问题标题】:Show UIWebView inside UINavigationController on a Modal view without NIB在没有 NIB 的模态视图上显示 UINavigationController 内的 UIWebView
【发布时间】: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];

}

到目前为止,它运行良好。现在我的问题:

  1. 我对 iOS 世界完全陌生。上面的代码是正确的还是有问题?
  2. 代码应该是使用带有 ARC 的 Xcode 4.2 编译的,所以我认为我不需要担心内存问题。
  3. 计算WebView初始大小的逻辑是否正确(我取主屏大小并减去导航栏高度)?
  4. 如何处理方向更改,以便在更改方向时,我的 Navigation View 和 WebView 都会调整到新的方向?

谢谢

编辑:

我尝试使用轮换来实现,但我无法让它发挥作用。 willRotateToInterfaceOrientation 没有在我的 WebViewController 上被调用,即使我从 shouldAutorotateToInterfaceOrientation 返回 YES(并且这个方法被调用)。当我尝试使用空笔尖(使用此空笔尖加载 WebViewController)时,它工作正常。

添加第 5 个要求:

  1. 另一个额外的要求是,当此模态对话框关闭时,呈现此对话框的视图应处于正确的方向(即,当模态对话框启动时,方向事件也会传递给呈现此模态视图的视图)。

谢谢

【问题讨论】:

    标签: iphone ipad uiwebview uinavigationcontroller orientation


    【解决方案1】:

    我的一些观点。

    1. 关于后退按钮,代替完成:

      self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];

    2. 导航栏高度始终为 44.0f

    3. 您可以使用以下方法处理方向更改:

      • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

      返回是; }

      • (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 持续时间:(NSTimeInterval)duration { [self adjustViewsForOrientation:toInterfaceOrientation]; // 在此处调整您的视图框架 }

    我不确定你的代码和内存问题 - 所以我无法评论这些。

    希望这些信息对您有所帮助。

    【讨论】:

    • 您好,mrunal,感谢您的提示:后退按钮进行了一些调整。我从 shouldAutorotateToInterfaceOrientation 返回 YES,但 willRotateToInterfaceOrientation 没有被调用。我想强调 WebViewController 视图本身就是 UIWebView。另一个额外的要求是,当这个模态对话框关闭时,呈现这个的视图应该处于正确的方向(即当模态对话框启动时,它也会将方向更改事件传递给呈现这个模态视图的视图)。
    【解决方案2】:

    我找到了解决旋转问题的方法。 替换

    - (id)init
    {
        return self;
    }
    

    - (id)init
    {
        if (self = [super init])
            return self;
        return nil;
    }
    

    并彻底删除以下方法:

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        return nil;
    }
    

    愚蠢的错误:) 现在它可以正常旋转了。

    还有人可以满足原来的要求吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-06
      • 1970-01-01
      相关资源
      最近更新 更多