PDA

View Full Version : Problem with a NSString that equals to (null)




guydor
Jun 17, 2010, 07:51 AM
Hi,

I have an UIViewController named MainViewController I have another UIViewController named LeftSharingViewController;

I would like to get and use the NSString from MainViewController in my LeftSharingViewController

I have a problem, I always get (null) instead of the NSString wanted value.

Here's my code and how does the NSString get it's value MainViewController:


- (void)webViewDidFinishLoad:(UIWebView *)webView {
leftWebViewString = [[NSString stringWithString:leftWebView.request.URL.absoluteString] retain];
}


LeftSharingViewController.h

#import <UIKit/UIKit.h>
#import "MainViewController.h"
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>

@class MainViewController;
@interface LeftSharingViewController : UIViewController <MFMailComposeViewControllerDelegate> {
MainViewController *mainViewController;
NSString *leftWebViewUrl;
}

@property (nonatomic, retain) MainViewController *mainViewController;
@property (nonatomic, retain) NSString *leftWebViewUrl;
@end


LeftSharingViewController.m

#import "LeftSharingViewController.h"
#import "MainViewController.h"

@implementation LeftSharingViewController
@synthesize mainViewController;
@synthesize leftWebViewUrl;
- (void)viewWillAppear:(BOOL)animated {
self.leftWebViewUrl = self.mainViewController.leftWebViewString;
}
#pragma mark -
#pragma mark Compose Mail
-(void)displayComposerSheet
{
MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
mailPicker.mailComposeDelegate = self;

[mailPicker setSubject:@"Check Out This Website!"];
[mailPicker setMessageBody:[NSString stringWithFormat:@"Take a look at this site:%@", leftWebViewUrl] isHTML:YES];

mailPicker.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:mailPicker animated:YES];
[mailPicker release];

}


Thanks!



robbieduncan
Jun 17, 2010, 07:59 AM
Where does the mainViewController property get set? In the code you have posted that never gets set so will be nil (and any message set to it will result in nil).

guydor
Jun 17, 2010, 08:18 AM
Where does the mainViewController property get set? In the code you have posted that never gets set so will be nil (and any message set to it will result in nil).

?

robbieduncan
Jun 17, 2010, 08:43 AM
?

:confused:

Anyway we could keep that going for ever :p

You call this in viewDidLoad:


self.leftWebViewUrl = self.mainViewController.leftWebViewString;


Where does mainViewController get set to anything. Declaring it as a property simply tells the compiler to save space for a pointer to this type of object. It does not magically guess which instance of this class you want the pointer to point at. So where do you call myLeftSharingViewControllerInsance.mainViewController = <something> (where myLeftSharingViewControllerInsance is the instance of LeftSharingViewController in question)?

guydor
Jun 17, 2010, 08:54 AM
:confused:

Anyway we could keep that going for ever :p

You call this in viewDidLoad:


self.leftWebViewUrl = self.mainViewController.leftWebViewString;


Where does mainViewController get set to anything. Declaring it as a property simply tells the compiler to save space for a pointer to this type of object. It does not magically guess which instance of this class you want the pointer to point at. So where do you call myLeftSharingViewControllerInsance.mainViewController = <something> (where myLeftSharingViewControllerInsance is the instance of LeftSharingViewController in question)?

Where should I declare it?

robbieduncan
Jun 17, 2010, 08:56 AM
Where should I declare it?

:confused: I'm beginning to think you need to step well away from the keyboard and look at the basics. I don't know where you are creating the instance of LeftSharingViewController but I would suggest that you set the property there and then (given that this property pretty much has to be set for this to work you really should be using a designated intialiser if you are creating it in code or linking it via an IBOutlet if using Interface Builder).

guydor
Jun 17, 2010, 09:05 AM
:confused: I'm beginning to think you need to step well away from the keyboard and look at the basics. I don't know where you are creating the instance of LeftSharingViewController but I would suggest that you set the property there and then (given that this property pretty much has to be set for this to work you really should be using a designated intialiser if you are creating it in code or linking it via an IBOutlet if using Interface Builder).

I declared LeftSharingViewController on the .h file of MainViewController


Thank you for your patience

robbieduncan
Jun 17, 2010, 09:08 AM
I declared LeftSharingViewController on the .h file of MainViewController

I don't see what that has to do with anything. The pointer in the instance of LeftSharingViewController is still nil.

guydor
Jun 17, 2010, 09:12 AM
I don't see what that has to do with anything. The pointer in the instance of LeftSharingViewController is still nil.

Thanks

robbieduncan
Jun 17, 2010, 09:20 AM
Thanks

Another way to do what? I already told you what you had to do: use the setter for the property to set the value to a non-nill value. As I said personally I'd be writing a designated initaliser. Something like:


- (id) initWithMainViewController:(MainViewController *) aMainViewController;


But that's because I do all my UI creation in code so when I wanted an instance of of LeftSharingViewController I would call the above with a pointer to the correct MainViewController instance. If you are creating the LeftSharingViewController in InterfaceBuilder than you'd be better making the mainViewController property an IBOutlet and connecting the two instances together there.

I also don't really know why you are bothering to have a property for leftWebViewUrl. Why not just get it from the mainViewController property when you need it?

If you actually answered my question: "where is the instance of LeftSharingViewController created?" then we might get somewhere. Given your inability to understand the most basic of terms I honestly think you need to stop trying to write code and step back to learning the basics of C, then Objective-C, then Cocoa/Cocoa Touch design paradigms. If you don't see why declaring a property is not the same as giving it a value I don't really know what can be done.