0

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

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

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

视图控制器.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;
}
4

2 回答 2

0

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

于 2013-03-28T17:26:11.733 回答
-1

我并不喜欢作者的所有决定,而是遵循他的模式来实现您正在寻找的东西。

添加了以下内容:

// 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];
于 2013-03-28T17:28:07.463 回答