简体   繁体   English

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

[英]How to correctly access a variable from another class in Objective C

I have looked at various forums trying to find a solution to this problem. 我看过各种论坛,试图找到解决此问题的方法。 I currently have 2 classes, BluetoothViewController and AccountViewController. 我目前有2个类,BluetoothViewController和AccountViewController。 What I am trying to do is pass an account that the user selects from the AccountViewController to BluetoothViewController, so I can use that string later in the BluetoothViewController. 我想做的是将用户从AccountViewController中选择的帐户传递给BluetoothViewController,以便稍后可以在BluetoothViewController中使用该字符串。 What I have done is create an instance of AccountViewController in BluetoothViewController.h and I have created a property for the string in the AccountViewController.h file. 我要做的是在BluetoothViewController.h中创建AccountViewController的实例,并在AccountViewController.h文件中为字符串创建了一个属性。 The string that I am trying to access is "account8" 我尝试访问的字符串是“ account8”

My AccountViewController.h file: 我的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;

My AccountViewController.m file: 我的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];

}

My BluetoothViewController.h 我的BluetoothViewController.h

#import "AccountViewController.h"

@interface BluetoothViewController : UIViewController <GKPeerPickerControllerDelegate, GKSessionDelegate>{

    AccountViewController   *classobj;

}


@property(nonatomic, retain) AccountViewController *classObj;

My BluetoothViewController.m file: 我的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;
}

When the user selects the row from a table, the contents will be saved in the variable account8. 当用户从表中选择行时,内容将保存在变量account8中。 BluetoothViewController will then be called. 然后将调用BluetoothViewController。 Now, when BluetoothViewController loads up, the account that was selected by the user is supposed to be shown. 现在,当BluetoothViewController加载时,应该显示用户选择的帐户。 However the label returns blank. 但是,标签返回空白。 I am wondering why it does not correctly access the variable saved in AccountViewController. 我想知道为什么它不能正确访问AccountViewController中保存的变量。

What you can do is to create an instance variable in Bluetoothcontroller and then after instantiating that object you set it from your AccountViewController. 您可以做的是在Bluetoothcontroller中创建一个实例变量,然后在实例化该对象后从AccountViewController对其进行设置。 For example: 例如:

AccountViewController: AccountViewController:

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

BluetoothViewController: BluetoothViewController:

@property (nonatomic, copy) NSString  *accountVariable;

Or are there any other reason you need to have classobj ? 还是有其他原因需要classobj

Please check code given below: 请检查下面给出的代码:

AccountViewController.h file: AccountViewController.h文件:

#import "AccountViewController.h"

@interface BluetoothViewController : UIViewController <GKPeerPickerControllerDelegate, GKSessionDelegate>{

    AccountViewController   *classobj;
}

@property(nonatomic, retain) AccountViewController *classObj;

BluetoothViewController.h file: BluetoothViewController.h文件:

#import "AccountViewController.h"

@interface BluetoothViewController : UIViewController <GKPeerPickerControllerDelegate, GKSessionDelegate>

@property(nonatomic, retain) AccountViewController *classObj;

AccountViewController.m file: 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 file: 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
}

Hope this will help you. 希望这会帮助你。

Using your XCode you need to make import, create object by declaring it as the property, and then use "object.variable" syntax. 使用XCode,您需要进行导入,通过将其声明为属性来创建对象,然后使用“ object.variable”语法。 The file "AccountViewController.m" would look in the following way: 文件“ AccountViewController.m”将以以下方式显示:

#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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM