Iphone 以编程方式添加UIView不起作用(UIView应显示UIWebView并打开URL)

Iphone 以编程方式添加UIView不起作用(UIView应显示UIWebView并打开URL),iphone,objective-c,uiview,uiwebview,Iphone,Objective C,Uiview,Uiwebview,目前正在为我的iPhone应用程序(如Pocket,供用户首次启动后亲自动手使用)制作两个介绍页面(第一个显示图像,第二个应该显示网站)。 遇到Matthew York的GitHub项目,该项目可以完美地处理图像和文本。 此外,我喜欢显示一个网站作为介绍的一部分,但我不能让它工作。我的以编程方式创建的UIWebView无法显示 Matthew York的GitHub项目: 显示两个简介页面的完整代码如下所示。 请注意,panelImage按预期工作,但不是panelView。 我看到第二个页面

目前正在为我的iPhone应用程序(如Pocket,供用户首次启动后亲自动手使用)制作两个介绍页面(第一个显示图像,第二个应该显示网站)。 遇到Matthew York的
GitHub
项目,该项目可以完美地处理
图像
文本
。 此外,我喜欢显示一个网站作为介绍的一部分,但我不能让它工作。我的
以编程方式创建的
UIWebView
无法显示

Matthew York的
GitHub
项目:

显示两个简介页面的完整代码如下所示。 请注意,
panelImage
按预期工作,但不是
panelView
。 我看到第二个页面出现了,但是没有
UIWebView
。 我想我把
子视图
添加到一个
视图
中,该视图在屏幕上不可见,因此我看不到它。我说得对吗?请看一下:
[view addSubview:aWebView]
方法中
showWebView

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;
}
-(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;
}
MYIntroductionPanel.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;
}
-(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;
}

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

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

增加了以下内容:

// 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    

}

@McClane发布的代码中有一个小错误,它有一个简单的解决方案。建议他放弃正在做的事情,从其他实现中复制粘贴几十行,至少可以说是适得其反的。@AaronGolden,你的解决方案说得很好,但不正确。请检查OP试图扩展的代码。您建议OP在MYIntroductionPanel上设置self.view=view。该类是NSObject的一个子类,没有视图属性(github作者选择的不是很好,但我们就是这样找到它的)。我没有从“其他实现”复制源代码。我不辞辛劳地获得了OP希望使用的github项目,理解它,并扩展它以满足他的需求。请查看问题中发布的代码。没错,github存储库中的类没有view属性,但问题中@McClane发布的代码确实分配给了
initWithView:description:
中的view属性,因此他可能在他的项目中扩展了该类。问题中发布的代码是错误的,这就是OP需要提问的原因。你被它误导了,不对。看见您建议设置self.view=view的类是NSObject的子类。它没有要设置的视图属性。@danh,@McClane当时一定扩展了该类。问题中发布的代码在
initWithView:description:
中设置视图属性。显然,@McClane使用的是一个版本的
MyIntroductionPanel
,它有一个视图属性。也许他扩展了它,或者只是添加了一个视图属性(或者他的代码没有编译)。这没有任何好处。获取github项目,自己看看。“panel”类只保留MyIntroductionView类的参数。那就是风景所在。它不会将面板类添加为子视图。(从名字上看,你的错误是可以理解的。不过,很抱歉,您的“琐碎”解决方案可能很琐碎,但它根本不是解决方案。