如何从Objective C中的另一个类正确访问变量

Sul*_*eed 1 xcode class object objective-c ios

我看过各种论坛试图找到解决这个问题的方法.我目前有2个类,BluetoothViewController和AccountViewController.我想要做的是将用户从AccountViewController中选择的帐户传递给BluetoothViewController,这样我以后可以在BluetoothViewController中使用该字符串.我所做的是在BluetoothViewController.h中创建一个AccountViewController实例,并在AccountViewController.h文件中为该字符串创建了一个属性.我要访问的字符串是"account8"

我的AccountViewController.h文件:

@interface AccountViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{

    NSArray     *tableData;
    int         cellValue;
    NSString    *cellContents;
    NSString    *account8;

}

@property (nonatomic, retain) NSArray   *tableData;
@property (nonatomic, retain) NSString  *cellContents;
@property (nonatomic, retain) NSString  *account8;
Run Code Online (Sandbox Code Playgroud)

我的AccountViewController.m文件:

#import "AccountViewController.h"


@interface AccountViewController ()

@end

// Implementing the methods of ViewController
@implementation AccountViewController

@synthesize tableData;
@synthesize cellContents;
@synthesize account8;


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
    account8 = [tableData objectAtIndex:indexPath.row];

    BluetoothViewController *bluetoothViewController = [[BluetoothViewController alloc] initWithNibName:@"BluetoothViewController" bundle:nil];
    [kAppDelegate setSelectedTabWithTag:-1];
    [self.navigationController pushViewController:bluetoothViewController animated:YES];

}
Run Code Online (Sandbox Code Playgroud)

我的BluetoothViewController.h

#import "AccountViewController.h"

@interface BluetoothViewController : UIViewController <GKPeerPickerControllerDelegate, GKSessionDelegate>{

    AccountViewController   *classobj;

}


@property(nonatomic, retain) AccountViewController *classObj;
Run Code Online (Sandbox Code Playgroud)

我的BluetoothViewController.m文件:

#import "AccountViewController.h"
#import "BluetoothViewController.h"



@interface BluetoothViewController ()

@end

// Implementing the methods of ViewController
@implementation BluetoothViewController

- (void)viewDidLoad {

    [connect setHidden:NO];
    [disconnect setHidden:YES];
    [super viewDidLoad];

    count = 0;

    classobj = [[AccountViewController alloc] init];

    accountSelected = classobj.account8;

    accountSelection.text = accountSelected;
}
Run Code Online (Sandbox Code Playgroud)

当用户从表中选择行时,内容将保存在变量account8中.然后将调用BluetoothViewController.现在,当BluetoothViewController加载时,应该显示用户选择的帐户.但是标签返回空白.我想知道为什么它没有正确访问AccountViewController中保存的变量.

Mik*_*ael 6

你可以做的是在Bluetoothcontroller中创建一个实例变量,然后在实例化该对象后,从AccountViewController中设置它.例如:

AccountViewController:

BluetoothViewController *bluetoothViewController = [[BluetoothViewController alloc] initWithNibName:@"BluetoothViewController" bundle:nil];
bluetoothViewController.accountVariable = _account8;
Run Code Online (Sandbox Code Playgroud)

BluetoothViewController:

@property (nonatomic, copy) NSString  *accountVariable;
Run Code Online (Sandbox Code Playgroud)

或者还有其他原因需要classobj吗?