views:

131

answers:

1

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];
}

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!

A: 

On your MainViewController, you are never retaining the NSString you create. You are using stringWithString:, which returns an autoreleased object.

You should either retain it, or (easier) change your code to use your accessor, which already retains, like this:

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

As a side note, if you are navigating from MainViewController to LeftSharingViewController, instead of "pulling" the data in LeftSharingViewController from MainViewController, I would do it the other way around: In MainViewController, before displaying the view of LeftSharingViewController, I'd set the required properties.

pgb
Hi,First thanks for answering meI did what you wrote but I still get (null)When you saying "retaining the NSString" do you me for this:@property (nonatomic, retain) NSString *leftWebViewString;?
Guy Dor
No, I'm pretty sure he means this: `leftWebViewString = [[NSString stringWithString:leftWebView.request.URL.absoluteString] retain];` If you don't use `self.leftWebViewString` (which is a short-cut for `[self setLeftWebViewString: ]`) then the property won't retain it.
Stephen Furlani
@Stephen Furlani: that's what I meant, yes.
pgb
Well, thank you both but I still get (null)
Guy Dor
Technically you should get `nil.`Try `retain` ing it one more time.
Stephen Furlani
NADAHere's my updated code:NSString *leftWebViewString;@propert (nonatomic, retain) NSString *leftWebViewString;leftWebViewString = [[NSString stringWithString:leftWebView.request.URL.absoluteString] retain];
Guy Dor
Maybe the problem is here, in this code of the LeftSharingViewController:- (void)viewWillAppear:(BOOL)animated { self.leftWebViewUrl = self.mainViewController.leftWebViewString;}
Guy Dor