UIView没有出现在UIScrollView中

时间:2012-07-24 08:39:16

标签: iphone objective-c ios uiscrollview uiimageview

我正在动态地将UIImage添加到我的UIScrollView对象中,一切都按预期工作。我现在需要添加额外的功能(当它从互联网加载时,UIActivityIndi​​cator位于图像的中心,下面是一个标签)所以我想为什么不创建一个自定义视图,其中包含所有这些,因为我需要它们是,然后只需将其加载到scrollView中。我遇到了一个问题,当我将它添加到scrollView时,没有任何显示。这就是我所拥有的:

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:

#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

#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(通过使用我的自定义类进行的尝试)中没有显示任何内容,滚动视图的第二页是通过向UIScrollView(工作)添加UIImageView进行的尝试。谁能指出我做错了什么?我不允许将自定义视图加载到UIScrollView中吗?谢谢你的帮助!

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

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

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

2 个答案:

答案 0 :(得分:1)

看起来CustomImageViewUIViewController上的子类,而不是UIImageViewUIView。您 无法 添加UIViewController作为子视图。将其更改为子类UIView,它应该可以工作。

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

- (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加载,因此您需要覆盖上面显示的方法并使用代码行

[[NSBundle mainBundle] loadNibNamed:@"<NIB NAME HERE>" owner:self options:nil];

加载笔尖。

继续你关于连接基本视图的评论,这就是我的意思:

创建自定义类和nib文件后,打开笔尖并突出显示左侧的“文件所有者”,然后在右上角,从左侧选择第3个图标,看起来像ID卡或其他内容。在“类”框中,添加自定义类的名称。

在customClass中,添加一个名为baseView的IBOutlet UIView属性,并在根级别添加一个覆盖IB视图的UIView,将这两个连接起来。然后在其上添加其他所有内容

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

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

#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

答案 1 :(得分:0)

如前所述,initWithNibName属于控制人员 我认为,你的方向是错误的。

你有一个ViewController,想要添加一个customView,从URLData动态加载图像。

您有2个选项:
选项1:在IB中执行所有操作:
在Interfacebuilder中编辑/(或添加一个新的)ViewController的XIB文件并添加您喜欢的视图。该文件的所有者是UIViewController类/子类。做你以前做的所有事情,除了在视图控制器中做。在你的App委托initWithNibName你的ViewController就像这样

    self.viewController = [[testViewController alloc] initWithNibName:@"testViewController" bundle:nil];

如果您愿意,请初始化您自己的视图控制器,您希望将其推入并将其推入堆栈。

您做错了什么:您正在使用Frame初始化customImageView,但是nib不会自动加载。属性在那里,但笔尖不是。

如果你真的想要一个稳定的东西选择
选项2:以编程方式执行所有操作(更简单,更理解,更轻松):
从您的实施中我们执行以下操作:

<强> NewsViewController.m

就像你之前做过的那样!!!

CustomImageView.h:

#import <UIKit/UIKit.h>

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

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

CustomImageView.m:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        caption = [[UILabel alloc] initWithFrame:CGRectMake(0, 250, 320, 50)];
        [caption setBackgroundColor:[UIColor greenColor]];
        imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 250)];
        [imageView setBackgroundColor:[UIColor blueColor]];
        activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        self.activityIndicator.frame = CGRectMake(320/2-25, 460/2-25, 50, 50);
        [self.activityIndicator startAnimating];


        [self addSubview:self.caption];
        [self addSubview:self.imageView];
        [self addSubview:self.activityIndicator];
    }
    return self;
}

然后做你以前做过的一切。 那样做会更好

相关问题