NSString被设置但从另一个类 - iOS调用时显示为空

时间:2012-02-29 23:27:56

标签: objective-c ios xcode nsstring get

我有一小段代码将类用户名的NSString( strUsername )设置为输入到UITextField中的值,然后按下一个按钮。这很好用: 的 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);
        }

我的NSLog输出显示UItextfield输入和NSString setter正在运行:

LocNews1[6699:707] inputUsername is: Harry brown 
LocNews1[6699:707] the username.strUsername is now: Harry brown

现在,当它点击此视图上的后退按钮时,它会返回到UITableViewController。然后我执行下拉刷新操作,并调用以下方法:

TestViewController.m(UITableViewController类型)

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

但是,这会将NSLog输出显示为:

LocNews1[6699:707] ViewController username.strUsername is:

正如你所看到的,已经在UsernameViewController.m中设置了字符串但是当我尝试在TestViewController.m中调用字符串时,它看起来是空白的(NSLog中没有空,只是空白);

导致这种情况的原因是什么?

编辑: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

注意:用户名在TestViewController.h和UsernameViewController.h中声明如下:Username * username;

1 个答案:

答案 0 :(得分:1)

UsernameViewController实例中的username实例变量与TestViewController实例中的username实例变量完全无关。您需要将变量存储在两个控制器都知道的位置,或者如果您希望它们都具有访问权限,则将它们传递给它们。简单地使用两个具有相同名称的变量并不会以任何方式连接它们。