Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

guydor

macrumors member
Original poster
Mar 10, 2009
67
0
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:

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

LeftSharingViewController.h
Code:
#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
Code:
#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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate

:confused:

Anyway we could keep that going for ever :p

You call this in viewDidLoad:

Code:
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

macrumors member
Original poster
Mar 10, 2009
67
0
Well, I don't really declared it

:confused:

Anyway we could keep that going for ever :p

You call this in viewDidLoad:

Code:
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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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

macrumors member
Original poster
Mar 10, 2009
67
0
Now I understanding you:

: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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate

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:

Code:
- (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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.