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];
}

customview.h

#import <UIKit/UIKit.h>

@class SubItemView;

@interface SubItemView : UIScrollView {

}


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

@end

customview.m

#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.

Answers


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

Aggregate Functions returning undesired information with joins

mysql join left-join aggregate-functions

I've been trying to run a really simple query in mysql, but I'm only getting one record. I'm getting all entries from a table and running aggregate functions on related data in another table. This ...

Convert string to hexadecimal in Ruby

ruby binary hex

I'm trying to convert a Binary file to Hexadecimal using Ruby.

How to specify the exact number of occurance of a token in ANTLR?

java parsing antlr grammar antlrworks

I have to define the grammar of a file like the one shown below.

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.