Passing Array and String to Custom UIView Class (Subclass) (iOS)

Hope you can help.

I have created my own Custom UIView and I'm trying to pass in a NSMutableArray which I have setup from data from a sqlite DB. I have NSLog'd the Array from the ViewController and everything is showing correctly.

However I want to pass this NSMutableArray into my Custom UIView (it's actually a UIScrollView) so that I can do some magic. However when I do this, my NSLog show's the output as (null).

Here is my code (I've also passed a test string to help to see if it's Array specific, but it isn't):

viewcontroller.m (just shown the Custom class call - NSLog outputs the Array contents (see end of examples)

- (void)viewDidLoad
NSString *teststring = @"Testing";
NSLog(@"Subitems: %@", subitems);
SubItemView* subitemview = [[SubItemView alloc] initWithFrame:CGRectMake(150,150,0,0)];
subitemview.cvSubitems = subitems;
subitemview.teststring = teststring;
[self.view addSubview:subitemview];


#import <UIKit/UIKit.h>

@class SubItemView;

@interface SubItemView : UIScrollView {


@property (nonatomic, retain) NSMutableArray *cvSubitems;
@property (nonatomic, retain) NSString *teststring;



#import "SubItemView.h"

@implementation SubItemView

@synthesize cvSubitems;
@synthesize teststring;

- (id)initWithFrame:(CGRect)frame
    CGRect rect = CGRectMake(0, 0, 400, 400);
    NSLog(@"Subclass Properties: %@", self.cvSubitems);
    self = [super initWithFrame:rect];

    if (self) {
        // Initialization code
    return self;

The first NSLog in the viewcontroller.m outputs:

Subitems: (
    "<SubItems: 0x6894400>",
    "<SubItems: 0x6894560>"

The second NSLog from the Custom UIScrollView outputs:

Subclass Properties: (null)

I am a bit of a newbie, so I'm obviously missing something (possibly obvious) here. I am just really struggling to pass an Array and even a simple string into a Custom class and just output it's contents via NSLog.

Any help is gratefully appreciated.


Well when your initWithFrame method is called, your cvSubitems property isn't set yet, as it is set only after your call to initWithFrame.

Try again to log your arrays value in a method that is called after your view is initialized, or provide a custom init method (e.g. initWithMyData: andFrame:) to solve this issue.

Need Your Help

How to raise a jquery event when an element with some class is created dynamically

javascript jquery html

Is it possible to raise a jquery event when an element with a certain class is created dynamically? This is what I mean. I have the following;

MySQL: What is a page?

mysql database innodb

I can't for the life of me remember what a page is, in the context of a MySQL database. When I see something like 8KB/page, does that mean 8KB per row or ...?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.