1

i have 2 classes in my app. View1 & View2; i want to use an object like array, strin, label.text in another class when i used them the shows null ;

View1.h:
{
NSMutableArray *array;
IBOutlet UILabel *lbl;
NSString *str;
}

@property(nonatomic,retain) NSMutableArray *array;
@property(nonatomic,retain)IBOutlet UILabel *lbl;
@property(nonatomic,retain) NSString *str;

@end;

View1.m

@synthesize array;
@synthesize lbl;
@synthesize str;


array = (1,2,3,..., nil) some dataa
str = @"HAI";
lbl.text = @"Text in label aaaa";

NSLog( @" %@", array );
NSLog( @" %@", lbl.text );
NSLog( @" %@", str );

gives correct out put

but in View2:

@implementation View2
    #import "View1"
    .
..
....
View1 *one = [View1 alloc]initwit.....................];

NSLog( @" %@", one.array );
NSLog( @" %@", one.lbl.text );
NSLog( @" %@", one.str );

prints null value

why ? what to do?

thanks in advance..

1
  • when you create the first instance of View1, how are you doing it? i.e. which init method has the code assigning values to array etc? looks like View2 is calling a different initializer. Commented Jan 19, 2012 at 10:05

2 Answers 2

2

Very easy. You can allocate this array in your AppDelegate, and then call it from your UIViews. Or you can allocate it in one of your UIViews and set point to this array into AppDelegate. Example:

//UIView 1 
NSArray *array = [[NSArray alloc] init];
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.mySharedArray = array;

//UIView2
 MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
 NSArray *array = appDelegate.mySharedArray;

Thats it

Sign up to request clarification or add additional context in comments.

4 Comments

did we need to create mySharedArray in appdelegate.h
You should create simple NSArray property in appdelegate.h, and then assign value to this property from your UIViews.
and u use same array "array" in view1, viw2. is these two are same? or does we declare a new array in view2?
Yes, they are will be same. This is point to your created array. Your welcome:)
1

Are you sure you give initial values to the variables from view1?

please make sure the init method for view1 gives some values to the argument, i.e. in

initwit..............

make sure you have:.

array = (1,2,3,..., nil) some dataa
str = @"HAI";
lbl.text = @"Text in label aaaa";

by the way, it is wrong code to initialize array the way you did, you should use one of NSArray initialize methods.

1 Comment

Yes i just gave the sample dataa in the array.. not mention all the syntax just casually typed as a question..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.