Note that there are some explanatory texts on larger screens.

plurals
  1. PONSString being set but appears empty when calling from another class - iOS
    primarykey
    data
    text
    <p>I have a small piece of code that sets the NSString (<em>strUsername</em>) of the class <strong>Username</strong> to the value which is typed into a UITextField and then a button is pressed. This works fine: <strong>UsernameViewController.m</strong></p> <pre><code> #import &lt;UIKit/UIKit.h&gt; #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); } </code></pre> <p>My NSLog output shows that the UItextfield input and NSString setter are working:</p> <pre><code>LocNews1[6699:707] inputUsername is: Harry brown LocNews1[6699:707] the username.strUsername is now: Harry brown </code></pre> <p>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:</p> <p><strong>TestViewController.m (UITableViewController type)</strong></p> <pre><code>#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]; } </code></pre> <p>However this shows the NSLog output as:</p> <pre><code>LocNews1[6699:707] ViewController username.strUsername is: </code></pre> <p>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);</p> <p>What could be causing this?</p> <p><strong>EDIT: Username.h/m</strong></p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @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 </code></pre> <p>Note: Username is declared in both TestViewController.h and UsernameViewController.h like: Username *username;</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload