我有一小段代码将Username类的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中没有null,只是空白);

是什么原因造成的?

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

最佳答案

UsernameViewController实例中的username实例变量与TestViewController实例中的username实例变量完全无关。如果您希望它们都可以访问,则需要将变量存储在两个控制器都知道的地方或在它们之间传递变量。仅具有两个相同名称的变量就不会以任何方式将它们连接起来。

10-05 23:11