keepsmiling
keepsmiling

Reputation: 66

How can we access values of an array added in a viewcontroller class, inside a different UIView class..?

viewcontroller.m has the following code

- (void)viewDidLoad
{
[super viewDidLoad];


self.array=[[NSArray alloc]initWithObjects:@"hi",@"hello", nil];
NSLog(@"%@",self.array);

view *view1=[[view alloc]init];
[view1 addSubview:self.view];
 view1.viewController=self;

}

and there is another UIView class where I am trying to access the array : the .h file :

#import <UIKit/UIKit.h>
#import "ViewController.h"

@class ViewController;

@interface view : UIView{
ViewController *viewController;


}
@property (nonatomic,retain)ViewController *viewController;


@end

and the .m file :

#import "view.h"
#import "ViewController.h"

@implementation view
@synthesize viewController;

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
    NSLog(@"%@",[viewController array]);
}

return self;
}

I checked in other posts of stackoverflow, and the passing of values was mentioned only between viewcontrollers; or the array was declared in the appdelegate and used in the classes(which I want to avoid).

The NSLog in the last code segment above gives null; so can you please help out in accessing the values of this array. Thanks in advance..!!

Upvotes: 0

Views: 428

Answers (3)

Dilip Manek
Dilip Manek

Reputation: 9143

You can achieve using this code in your ViewController

#import "view.h"

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSArray *ary = [NSArray arrayWithObjects:@"7",@"5",@"3",@"2", nil];

    view *v=[[view alloc] init];
    [v initView:ary];

}

And in your view.h file :

#import <UIKit/UIKit.h>

@interface view : UIView

-(void)initView:(NSArray *)ary;
@end

And in your .m file :

#import "view.h"
#import "ViewController.h"

@implementation view

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

    }
    return self;
}

-(void)initView:(NSArray *)ary
{
    NSLog(@"%@",ary);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

Log value will display this :

2013-02-20 20:11:52.731 SampleProject[9414:f803] (
    7,
    5,
    3,
    2
)

Upvotes: 2

Rakesh
Rakesh

Reputation: 3399

First thing you are calling the init method on the view and checking for viewController in the initWithFrame method which is never called. (But maybe you are calling the initWithFrame: from inside your init method with a default frame. :) ).
Second, you are setting the viewcontroller property after you have called the init method, so your viewcontroller is still uninitialized in your initWithFrame method. Third, instead of passing the whole of viewcontroller to your view to access the array (which kind of goes against MVC pattern), you could probably use just create an instance variable in your UIView subclass and pass just the array.

Then you could follow the answer given by Dilip, preferably using the setter method for setting the array. IMO.

Upvotes: 0

A-Live
A-Live

Reputation: 8944

This line

view *view1=[[view alloc]init];

calls the desired initializer initWithFrame: before you set view1.viewController, so what's happening is that

NSLog(@"%@",[viewController array]);

actually calls

NSLog(@"%@",[null array]);

or (note that is pseudocode)

NSLog(@"%@",null);

What you'll want to do is to use view1.viewController after it is assigned. The best practice would be to make a custom constructor taking UIViewController* as a parameter and use it.

Upvotes: 1

Related Questions