Reputation: 12051
A really noob question here.
I have 2 VCs.
One is called SGPlayer
and another is SGDownloader
. Basically I want to change the property of SGDownloader
from the first class.
I import the SGDownloader
class like this:
// SGPlayer.h
#import <UIKit/UIKit.h>
#import "SGDownloader.h"
@interface SGPlayer : UIViewController
@property (nonatomic, retain) SGDownloader *downloaderClass;
SGPlayer.m
is like this:
// SGPlayer.m
#import "SGPlayer.h"
@interface SGPlayer ()
@end
@implementation SGPlayer
@synthesize downloaderClass;
I then set the downloadUrl property form the SGDownloader
class in an action triggered by UIButton
like that:
- (IBAction)loadURLButton:(id)sender {
[self performSegueWithIdentifier:@"loadMyData" sender:nil];
downloaderClass.downloadURL = [NSURL URLWithString:@"http://www.google.com"];
}
NSLog
gives me (null) on my URL. If I use prepareForSegue
method all is fine:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"loadMyData"])
{
SGDownloader *vc = [segue destinationViewController];
vc.downloadURL = [NSURL URLWithString:@"http://www.google.com"];
}
}
The thing is I don't want to use prepareForSegue
here.
I assume I might be misunderstanding the whole concept of how do you transfer the data between classes in Obj-C.
Edit:
I do have a synthesised property in the SGDownloader
class:
@interface SGDownloader : UIViewController
@property (nonatomic, retain) NSURL *downloadURL;
@end
Upvotes: 1
Views: 447
Reputation: 14068
Unfortunately yo do not show the full code. I guess that if your self.downloaderClass
is not nil, as suggested before, then it is a different instance of oyur view controller than segue.destinationViewController
. You may have created an instance but that does not stop storyboard from creating its own.
I'd rather suggest to use prepareForSegue
when using storyboard. Simply because this method is designed for exactly this purpose.
BTW, downloaderClass
is not the smartest name for that object, because it does not refer to a class (that would be a property of type Class*) but to an acutal instance. downloadViewController
or so would be more appropriate in terms of "speaking code". However, this does not cause your problem.
And, of course, downloadURL must be accessible from outside that class. That needs to be a property too. On the other hand it seems to be derrived from a constant string value. If that is true and remains true while your project carries on, then there is no point in setting the value from outside. You could as well set it in an appropriate init method within the class.
Upvotes: 2
Reputation: 747
As mentioned above your downloaderClass
is nil when you try and set the property. So before you set it you can use self.downloaderClass = [[SGDownloader alloc] init....]
.
You then have to make sure when you switch view controllers that you are using this instance and not one instantiated by the storyboard (cant help here as i havent used storyboards).
Upvotes: 3
Reputation: 2237
Either create property of downloadURL and synthesize it or you can use delegate to pass values between class
Upvotes: 3
Reputation: 7343
It seems that your downloaderClass
property is nil
, not just it's downloadURL
. That is why you get null. In the prepareForSegue
it is instantiated for you from the storyboard, I recommend you use it there.
Hope this helps!
Upvotes: 2
Reputation: 1742
You need to provide memory to your SGDownloader *downloaderClass
before you assign a property.
Use alloc before you use that downloaderClass
.
Upvotes: -1
Reputation: 31091
Hi make property of downloadURL
@property (nonatomic, retain) NSUrl * downloadURL;
Upvotes: 3