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

时间:2013-01-23 15:29:18

标签: ios objective-c xcode class object

我看过各种论坛试图找到解决这个问题的方法。我目前有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;

我的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];

}

我的BluetoothViewController.h

#import "AccountViewController.h"

@interface BluetoothViewController : UIViewController <GKPeerPickerControllerDelegate, GKSessionDelegate>{

    AccountViewController   *classobj;

}


@property(nonatomic, retain) AccountViewController *classObj;

我的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;
}

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

3 个答案:

答案 0 :(得分:6)

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

AccountViewController:

BluetoothViewController *bluetoothViewController = [[BluetoothViewController alloc] initWithNibName:@"BluetoothViewController" bundle:nil];
bluetoothViewController.accountVariable = _account8;

BluetoothViewController:

@property (nonatomic, copy) NSString  *accountVariable;

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

答案 1 :(得分:1)

请检查以下代码:

AccountViewController.h文件:

#import "AccountViewController.h"

@interface BluetoothViewController : UIViewController <GKPeerPickerControllerDelegate, GKSessionDelegate>{

    AccountViewController   *classobj;
}

@property(nonatomic, retain) AccountViewController *classObj;

BluetoothViewController.h文件:

#import "AccountViewController.h"

@interface BluetoothViewController : UIViewController <GKPeerPickerControllerDelegate, GKSessionDelegate>

@property(nonatomic, retain) AccountViewController *classObj;

AccountViewController.m文件:

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

        BluetoothViewController *bluetoothViewController = [[BluetoothViewController alloc] initWithNibName:@"BluetoothViewController" bundle:nil];

        [kAppDelegate setSelectedTabWithTag:-1];
[bluetoothViewController.view setAlpha:1.0f];

[bluetoothViewController setClassObj:self];

        [self.navigationController pushViewController:bluetoothViewController animated:YES];

    }

BluetoothViewController.m文件:

- (void)viewDidLoad {

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

    count = 0;

// Here classObj is already assigned to a reference so you can directly use that one
}

希望这会对你有所帮助。

答案 2 :(得分:0)

使用你的XCode你需要进行导入,通过将其声明为属性来创建对象,然后使用&#34; object.variable&#34;句法。文件&#34; AccountViewController.m&#34;将以下列方式看待:

#import AccountViewController.h
#import BluetoothViewController.h;

@interface AccountViewController ()
...
@property (nonatomic, strong) BluetoothViewController *bluetoothViewController;
...
@end

@implementation AccountViewController

//accessing the variable from balloon.h
...bluetoothViewController.variableFromBluetoothViewController...;

...
@end