【问题标题】:Adding an UIView programmatically not working (UIView should show an UIWebView and open URL)以编程方式添加 UIView 不起作用(UIView 应显示 UIWebView 并打开 URL)
【发布时间】:2013-03-28 16:48:51
【问题描述】:

目前正在为我的 iPhone 应用程序(如 Pocket,用户在第一次启动后进行一些动手操作)制作两个介绍页面(第一个显示图像,第二个应该显示一个网站)。 遇到了 Matthew York 的 GitHub 项目,它与 imagestext 完美配合。 此外,我喜欢在介绍中展示一个网站,但我无法让它发挥作用。我的programmatically 创建的UIWebView 只是没有出现。

Matthew York 的GitHub 项目: https://github.com/MatthewYork/iPhone-IntroductionTutorial

显示两个介绍页面的完整代码如下所示。 请注意panelImage 可以按预期工作,但panelView 不能。 我看到第二页出现了,但没有UIWebView。 我想我将subview 添加到屏幕上不可见的view,因此我看不到它。我对吗?你能看看:[view addSubview:aWebView];methodshowWebView

ViewController.m

- (void)showIntro
{
    MYIntroductionPanel *panelImage = [[MYIntroductionPanel alloc] initWithimage:[UIImage imageNamed:@"img.jpg"] description:@"TEST: IMAGE"];

    MYIntroductionPanel *panelView = [[MYIntroductionPanel alloc] initWitView:[self showWebView] description:@"TEST: VIEW"];

    MYIntroductionView *introductionView = [[MYIntroductionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) headerText:@"TESTING" panels:@[panelImage, panelView] languageDirection:MYLanguageDirectionLeftToRight];

    introductionView.BackgroundImageView.image = [UIImage imageNamed:@"BG_iPad_1024.png"];
    introductionView.delegate = self;
    [introductionView showInView:self.view];
}

- (UIView *)showWebView
{
    UIWebView *aWebView= [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 290)];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
    [aWebView loadRequest:requestObj];

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
    [view addSubview:aWebView];

    return view;
}

MYIntroductionPanel.m

-(id)initWitView:(UIView *)view description:(NSString *)description{
    if (self = [super init]) {
        self.view = [[UIView alloc] init];
        self.Description = [[NSString alloc] initWithString:description];
    }
    return self;
}

-(id)initWithimage:(UIImage *)image title:(NSString *)title description:(NSString *)description{
    if (self = [super init]) {
        self.Image = [[UIImage alloc] init];
        self.Image = image;
        self.Title = title;
        self.Description = [[NSString alloc] initWithString:description];
    }
    return self;
}

【问题讨论】:

    标签: iphone objective-c uiview uiwebview


    【解决方案1】:

    您添加UIWebView 的视图永远不会添加到视图层次结构本身。您似乎正在将 UIWebView 的超级视图传递给 MyIntroductionPanel 的 initWithView:description: 方法,但该方法只是忽略其输入视图并使用 self.view = [[UIView alloc] init] 创建一个新视图。尝试在您的 initWithView:description: 方法中设置 self.view = view

    【讨论】:

    • 不正确。见github.com/MatthewYork/iPhone-IntroductionTutorial/blob/master/…。您建议设置 self.view = view 的类是 NSObject 的子类。它没有要设置的视图属性。
    • @danh, @McClane 那时肯定已经扩展了这个类。问题中发布的代码在initWithView:description: 中设置了视图属性。显然,@McClane 正在使用具有 view 属性的 MyIntroductionPanel 版本。
    • 也许他扩展了它或者只是添加了一个视图属性(或者也许他的代码无法编译)。它不会有任何好处。获取 github 项目并亲自查看。 “面板”类只保留 MyIntroductionView 类的参数。这就是观点所在。它不会将面板类添加为子视图。 (人们从名称中假设的方式。鉴于命名不佳,您在这里的错误是可以理解的)。不过很抱歉,您的“微不足道”的解决方案可能微不足道,但根本不是解决方案。
    【解决方案2】:

    我并不喜欢作者的所有决定,而是遵循他的模式来实现您想要的。

    添加了以下内容:

    // MyIntroductionPanel.h
    @property (nonatomic, retain) NSURL *url;
    
    -(id)initWithURL:(NSURL *)url;
    
    // MyIntroductionPanel.m
    -(id)initWithURL:(NSURL *)url {
    
        if (self = [super init]) {
            _url = url;
        }
        return self;
    }
    

    这很容易。这不太容易 - 破解布局方法来处理带有 url 的面板。我没有太努力去理解这种方法,只是添加了我能做到的东西来让它工作。我的添加标记为 // DANH

    //  MYIntroductionView.m
    
    -(UIView *)PanelViewForPanel:(MYIntroductionPanel *)panel atXIndex:(CGFloat*)xIndex{
    
        // snipped original code    
    
        NSInteger descriptionHeight = panelDescriptionTextView.contentSize.height;
        int contentWrappedScrollViewHeight = 0;
        if ((imageHeight + descriptionHeight + panelTitleLabelFrame.size.height) > maxScrollViewHeight) {
            contentWrappedScrollViewHeight = maxScrollViewHeight;
            imageHeight = contentWrappedScrollViewHeight-descriptionHeight - panelTitleLabelFrame.size.height - 10;
        }
        else if ((imageHeight+descriptionHeight + panelTitleLabelFrame.size.height) <= maxScrollViewHeight){
            contentWrappedScrollViewHeight = imageHeight + panelTitleLabelFrame.size.height + descriptionHeight;
        }
    
        // DANH
        if (panel.url) {
            contentWrappedScrollViewHeight = 300;
            // you can certainly do a better job than I did here, but just to get going
        }
    
        panelView.frame = CGRectMake(*xIndex, 0, self.ContentScrollView.frame.size.width, contentWrappedScrollViewHeight);
    
        //Build image container
        UIImageView *panelImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 0, self.ContentScrollView.frame.size.width - 10, imageHeight)];
        panelImageView.contentMode = UIViewContentModeScaleAspectFit;
        panelImageView.backgroundColor = [UIColor clearColor];
        panelImageView.image = panel.Image;
        panelImageView.layer.cornerRadius = 3;
        panelImageView.clipsToBounds = YES;
        [panelView addSubview:panelImageView];
    
        // DANH
        // add webview on top if we have a url
        if (panel.url) {
            UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.ContentScrollView.frame.size.width, 300)];
            [webView loadRequest:[NSURLRequest requestWithURL:panel.url]];
            [panelView addSubview:webView];
        }
    
        // snipped original code    
    
    }
    

    现在,用示例代码调用它......

    // MYViewController.m
    
    MYIntroductionPanel *panel3 = [[MYIntroductionPanel alloc] initWithURL:[NSURL URLWithString:@"http://www.google.com"]];
    
    // ...
    MYIntroductionView *introductionView = [[MYIntroductionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) headerText:@"MYIntroductionView" panels:@[panel, panel2, panel3] languageDirection:MYLanguageDirectionLeftToRight];
    

    【讨论】:

    • @McClane 发布的代码中存在一个微不足道的错误,并提供了一个微不足道的解决方案。建议他放弃他正在做的事情并从其他实现中复制粘贴数十行至少可以说适得其反。
    • @AaronGolden,您的解决方案说得很好,但不正确。请查看 OP 尝试扩展的代码。您建议 OP 在 MYIntroductionPanel 上设置 self.view = view。该类是 NSObject 的子类,并且没有视图属性(github 作者不是一个很好的选择,但我们就是这样找到它的)。我没有从“其他一些实现”复制源代码。我不厌其烦地得到了 OP 希望使用的 github 项目,理解并扩展它以满足他的要求。
    • 请查看问题中发布的代码。没错,github 存储库中的类上没有视图属性,但@McClane 在问题中发布的代码确实分配给initWithView:description: 中的视图属性,因此他可能在他的项目中扩展了该类。
    • 问题中发布的代码有误,这就是OP需要提问的原因。你被它误导了。
    猜你喜欢
    • 2011-10-27
    • 2016-01-22
    • 2019-07-25
    • 2014-06-27
    • 2013-04-08
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多