Reputation: 1534
I have a small piece of code that sets the NSString (strUsername) of the class Username to the value which is typed into a UITextField and then a button is pressed. This works fine: UsernameViewController.m
#import <UIKit/UIKit.h>
#import "Username.h"
@interface UsernameViewController : UIViewController
{
Username *username;
__weak IBOutlet UITextField *test;
}
@property (nonatomic, retain)IBOutlet UITextField *txtUsername;
@property (nonatomic, retain) IBOutlet UITextField *test;
-(IBAction)setUsername;
@end
#import "UsernameViewController.h"
#import "Username.h"
@implementation UsernameViewController
@synthesize test=_test;
- (void)viewDidLoad
{
[super viewDidLoad];
username = [[Username alloc] init];
}
-(IBAction)setUsername
{
NSString *inputUsername = self.test.text;
NSLog(@"inputUsername is: %@", inputUsername);
username.strUsername = inputUsername;
NSLog(@"the username.strUsername is now: %@", username.strUsername);
}
My NSLog output shows that the UItextfield input and NSString setter are working:
LocNews1[6699:707] inputUsername is: Harry brown
LocNews1[6699:707] the username.strUsername is now: Harry brown
Now when it hit the back button on this view it return me back to a UITableViewController. I then perform a pull down to refresh action and its called the following method:
TestViewController.m (UITableViewController type)
#import "TestViewController.h"
#import "ViewController.h"
#import "DetailViewController.h"
#import "AppDelegate.h"
#import "NewsArticle.h"
#import "ResultsCustomCell.h"
#import "XMLParser.h"
@implementation TestViewController
//some code
- (void)addItem
{
username = [[Username alloc] init];
// Use XMLparser to check for updated new feeds.
if(username.strUsername != NULL)
{
NSLog(@"ViewController username.strUsername is:%@",username.strUsername);
activeUsername = username.strUsername;
}
else
{
NSLog(@"%@",username.strUsername);
activeUsername = @"";
}
NSString *myLat = [[NSString alloc] initWithFormat:@"%f", locationManager.location.coordinate.latitude];
NSString *mylong = [[NSString alloc] initWithFormat:@"%f", locationManager.location.coordinate.longitude];
XMLParser *parseQuestionnaire = [[XMLParser alloc] init];
NSLog(@"username %@",activeUsername);
newsArticle = [parseQuestionnaire parseXML:myLat:mylong:activeUsername];
[self.tableView reloadData];
[self stopLoading];
}
However this shows the NSLog output as:
LocNews1[6699:707] ViewController username.strUsername is:
As you can see the string has been set in UsernameViewController.m but when I try and call the string back in TestViewController.m is appears to be blank (there is no null in the NSLog, just blank);
What could be causing this?
EDIT: Username.h/m
#import <Foundation/Foundation.h>
@interface Username : NSObject
{
NSString *strUsername;
}
@property (nonatomic, retain) NSString *strUsername;
@end
#import "Username.h"
@implementation Username
@synthesize strUsername;
-(id)init
{
strUsername = [[NSString alloc] init];
return self;
}
@end
Note: Username is declared in both TestViewController.h and UsernameViewController.h like: Username *username;
Upvotes: 1
Views: 501
Reputation: 237110
The username
instance variable in that instance of UsernameViewController is completely unrelated to the username
instance variable in the instance of TestViewController. You'll need to store the variable in a place that both controllers know about or pass it between them if you want them both to have access. Simply having two variables with the same name doesn't connect them in any way.
Upvotes: 1