开发者

Problem with a NSString that equals to (null)

开发者 https://www.devze.com 2023-01-03 07:59 出处:网络
I have an UIViewController named MainViewController I have another UIViewController named LeftSharingViewController;

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

ma开发者_StackOverflow社区ilPicker.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:mailPicker animated:YES];
[mailPicker release];

}

Thanks!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消