UIView没有显示在UIScrollView中 [英] UIView is not showing up in UIScrollView

查看:49
本文介绍了UIView没有显示在UIScrollView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在动态地将UIImage添加到我的UIScrollView对象中,并且一切都按预期工作.我现在需要添加额外的功能(从Internet加载图像时,UIActivityIndi​​cator位于图像中央,下方是标签),所以我想为什么不创建一个自定义视图,将所有这些图像布置好,因为我需要它们,然后将其加载到scrollView中.但是,当我将其添加到scrollView时,没有遇到任何问题.这是我所拥有的:

I am currently dynamically adding a UIImage to my UIScrollView object and everything is working as intended. I need to add extra functionality now (a UIActivityIndicator in the center of the image when it is loading from the internet, and a label underneath) so I thought why not create a custom View which has all of these laid out as I need them to be, and then just load it into the scrollView. I have encountered a problem though, when I add it to the scrollView, nothing shows up. Here is what I have:

NewsViewController.m:

NewsViewController.m:

imageScrollView.contentSize = CGSizeMake(320*numberOfPages, 303);
pageControl.numberOfPages = numberOfPages;
dispatch_queue_t imageDownload = dispatch_queue_create("imageDownload", NULL);
__block NSData *temp;
    CustomImageView *customImageView = [[CustomImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 303)];
    [imageScrollView addSubview:customImageView];
    [[customImageView activityIndicator] startAnimating];
    dispatch_async(imageDownload, ^{
        temp = [[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.wlfarm.org/wp-content/uploads/2012/05/farm.jpg"]]retain];
        dispatch_async(dispatch_get_main_queue(), ^{
            customImageView.imageView.image = [[UIImage alloc] initWithData:temp];
            [[customImageView activityIndicator] stopAnimating];
            [customImageView setNeedsDisplay];
            customImageView.caption.text = @"HAHAHAHAHHAHA";
            [imageScrollView setNeedsDisplay];
            [temp release];
        });
    });

    dispatch_release(imageDownload);

CustomImageView.h:

CustomImageView.h:

#import <UIKit/UIKit.h>

@interface CustomImageView : UIView
{
    IBOutlet UIImageView *imageView;
    IBOutlet UILabel *caption;
    IBOutlet UIActivityIndicatorView *activityIndicator;
}

@property (nonatomic, retain) IBOutlet UIImageView *imageView;
@property (nonatomic, retain) IBOutlet UILabel *caption;
@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *activityIndicator;
@end

CustomImageView.m

CustomImageView.m

#import "CustomImageView.h"  
@interface CustomImageView ()

@end

@implementation CustomImageView

@synthesize caption, imageView, activityIndicator;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

 - (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:    (UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

我包括我的XIB文件的屏幕截图,以及在模拟器上运行的程序.第一幅图片在scrollView中什么都没有显示(使用我的自定义类进行的尝试),第二幅滚动视图是通过将UIImageView添加到UIScrollView进行的尝试(有效).谁能指出我做错了什么?我是否不允许将自定义视图加载到UIScrollView中?感谢您的帮助!

I am including a screenshot of my XIB file, and the program running on the simulator. The first picture shows nothing in the scrollView(the attempt made by using my custom class), and the second page of the scroll view is the attempt made by adding a UIImageView to the UIScrollView(which worked). Can anyone point out what I am doing wrong? Am I not allowed to load a custom view into a UIScrollView? Thanks for your help!

IB屏幕截图- http://i.stack.imgur.com/gz9UL.png

iOS模拟器没有带CustomView屏幕截图的图像- http://i.stack.imgur.com/zhswq.png

iOS Simulator No Image with CustomView Screenshot - http://i.stack.imgur.com/zhswq.png

带有UIImageView屏幕截图的iOS模拟器图像- http://i.stack.imgur.com/97vmU.png

iOS Simulator Image with UIImageView Screenshot - http://i.stack.imgur.com/97vmU.png

推荐答案

CustomImageView 似乎是 UIViewController 上的子类,而不是 UIImageView UIView .您 不能 UIViewController 添加为这样的子视图.将其更改为子类 UIView ,它应该可以工作.

It looks as though CustomImageView is a subclass on UIViewController, not UIImageView or UIView. You can't add a UIViewController as a subview like that. change it to subclass UIView and it should work.

要从.nib加载 UIView ,您需要声明 IBOutlet 属性,就像声明 UIViewController 一样.在IB中定义正确的自定义类,并连接所有内容,包括基本视图.然后在您的自定义视图类中重写以下方法.

To load a UIView from a .nib, you need to declare your IBOutlet properties as you would for a UIViewController. Define the correct custom class in IB and connect everything up, including the base view. Then override the following method inside your custom view class.

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
    // Initialization code.
    //
    [[NSBundle mainBundle] loadNibNamed:@"<NIB NAME HERE>" owner:self options:nil];
    [self addSubview:self.view];
    }
  return self;
}

似乎您已将方法从 UIViewController 子类剪切并粘贴到您的 UIView 子类中.首先删除在 @synthesize @end 之间粘贴的所有方法. UIView 子类需要不同的方法才能从.nib加载,因此您需要改写上面显示的方法,并使用代码行

It seems you have cut and pasted methods from a UIViewController subclass into you UIView subclass. Start by deleting all the methods you've pasted in between @synthesize and @end . A UIView subclass requires different methods to to load from a .nib, so you need to override the method shown above instead and use the line of code

[[NSBundle mainBundle] loadNibNamed:@< NIB NAME HERE>"owner:self选项:无];

加载笔尖.

对于您关于连接基本视图的评论,这是我的意思:

Further to your comment about connecting the base view, here is what I mean:

创建自定义类和笔尖文件后,打开笔尖并突出显示左侧的文件所有者",然后在右上角从左侧选择第3个图标,看起来像身份证或其他东西.在类"框中,添加自定义类的名称.

After creating your custom class and nib file, open the nib and highlight "Files Owner" on the left, then on the top right hand side, select the icon 3rd from the left, looks like an ID card or something. In the "Class" box, add the name of your custom class.

在您的customClass中,添加一个名为baseView的 IBOutlet UIView 属性,并添加一个在根级别覆盖IB中视图的UIView,将这两个连接起来.然后在此

In your customClass, add an IBOutlet UIView property called baseView, and add a UIView that covers the view in IB at the root level, connect these two. Then add everything else on top of that

您的代码现在看起来像这样:

you code would now look something like this:

CustomImageView.h

CustomImageView.h

#import <UIKit/UIKit.h>

@interface CustomImageView : UIView
{
    IBOutlet UIView *baseView
    IBOutlet UIImageView *imageView;
    IBOutlet UILabel *caption;
    IBOutlet UIActivityIndicatorView *activityIndicator;
}
@property (nonatomic, retain) IBOutlet UIView *baseView;
@property (nonatomic, retain) IBOutlet UIImageView *imageView;
@property (nonatomic, retain) IBOutlet UILabel *caption;
@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *activityIndicator;
@end

CustomImageView.m

CustomImageView.m

#import "CustomImageView.h"  
@interface CustomImageView ()

@end

@implementation CustomImageView

@synthesize baseView, caption, imageView, activityIndicator;

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
    // Initialization code.
    //
    [[NSBundle mainBundle] loadNibNamed:@"<NIB NAME HERE>" owner:self options:nil];
    [self addSubview:self.baseView];
    }
  return self;
}

@end

只要您将其全部连接到IB中,它就可以正常工作,只记得添加baseView

Provided you connect it all up in IB it should work fine, just remember to add the baseView

这篇关于UIView没有显示在UIScrollView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆