Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-15-2010, 04:10 PM   #1 (permalink)
Yko
Registered Member
 
Join Date: Sep 2010
Posts: 26
Yko is on a distinguished road
Default Passing a pointer to a second class to modify the data but didn't work!

Hey guys,

Any idea why this doesn't work?

Quote:
#import SecondClass.h

@interface FirstClass : UIViewController
{
IBOutlet UIButton *doSomething;
IBOutlet SecondClass *secondClass;
NSString *firstClassString;
}
-(IBAction) doSomethingPressed;
@property (nonatomic, retain) NSString *FirstClassString
@implementation FirstClass
@synthesize FirstClassString
-(IBAction)doSomethingPressed
{
[secondClass setSecondClassString:firstClassString];
}

// Second Class
@interface SecondClass : UIViewController{
NSString *secondClassString;
}
@property (nonatomic, retain) NSString *secondClassString;
@implementation SecondClass;
@synthesize secondClassString;

// another IBAction which leads to:
[self setSecondClassString:@"this should change the firstClassString, no?"];
I use a similar method to pass an array and it works. The object inside the array gets changed but not this string. Why is that?

Thanks!
Yko is offline   Reply With Quote
Old 09-15-2010, 06:55 PM   #2 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
RLScott is on a distinguished road
Default

No, it should not change FirstClassString, and here is why.

Both FirstClassString and SecondClassString are pointers to NSString objects. At the time when you do

Code:
[secondClass setSecondClassString:firstClassString];
This makes both pointers point at the same NSString. The two pointers are still distinct pointers. Neither pointer points at the other pointer. They just both point at the same object. Then when you do

Code:
[self setSecondClassString:@"this should change the firstClassString, no?"];
That makes SecondClassString point somewhere new. It does not change the object that it was pointing at. And FirstClassString is still pointing at the old object. So nothing changes, as far as FirstClassString is concerned.
RLScott is offline   Reply With Quote
Old 09-16-2010, 08:24 AM   #3 (permalink)
Yko
Registered Member
 
Join Date: Sep 2010
Posts: 26
Yko is on a distinguished road
Default

Quote:
Originally Posted by RLScott View Post
No, it should not change FirstClassString, and here is why.

Both FirstClassString and SecondClassString are pointers to NSString objects. At the time when you do

Code:
[secondClass setSecondClassString:firstClassString];
This makes both pointers point at the same NSString. The two pointers are still distinct pointers. Neither pointer points at the other pointer. They just both point at the same object. Then when you do

Code:
[self setSecondClassString:@"this should change the firstClassString, no?"];
That makes SecondClassString point somewhere new. It does not change the object that it was pointing at. And FirstClassString is still pointing at the old object. So nothing changes, as far as FirstClassString is concerned.
Thanks for the explanation but then why does it work with NSMutableArrays? For example,

NSMutableArray firstClassArray;
NSMutableArray secondClassArray;

...
...
...

[secondClass setSecondClassArray:firstClassArray];

...

then in the second class, you do something like this:
[secondClassArray insertObject:anObject atIndex:0];

...

in my first class, the firstClassArray @ index 0 will get changed to anObject.




Maybe, I am overthinking stuff but if you can show me how I can pass NSString from one view controller to another VC, change the value, and then pass back that value, I would appreciate it. Right now, I'm using the NSMutableArray because the NSString doesn't change in the first class.

Thanks!
Yko is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 329
19 members and 310 guests
2ndSegment, bignoggins, cayladv57, cgokey, dermotos, djohnson, Domele, guusleijsten, Hamad, heshiming, linkmx, markuschow, Objective Zero, pungs, Rudy, Sloshmonster, teebee74, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,655
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, pungs
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:54 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0