Sulaiman Majeed
Sulaiman Majeed

Reputation: 499

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. 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. 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. The string that I am trying to access is "account8"

My AccountViewController.h file:

@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:

#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

#import "AccountViewController.h"

@interface BluetoothViewController : UIViewController <GKPeerPickerControllerDelegate, GKSessionDelegate>{

    AccountViewController   *classobj;

}


@property(nonatomic, retain) AccountViewController *classObj;

My BluetoothViewController.m file:

#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. BluetoothViewController will then be called. Now, when BluetoothViewController loads up, the account that was selected by the user is supposed to be shown. However the label returns blank. I am wondering why it does not correctly access the variable saved in AccountViewController.

Upvotes: 1

Views: 21148

Answers (3)

Darius Miliauskas
Darius Miliauskas

Reputation: 3524

Using your XCode you need to make import, create object by declaring it as the property, and then use "object.variable" syntax. The file "AccountViewController.m" would look in the following way:

#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

Upvotes: 0

Mikael
Mikael

Reputation: 3612

What you can do is to create an instance variable in Bluetoothcontroller and then after instantiating that object you set it from your AccountViewController. For example:

AccountViewController:

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

BluetoothViewController:

@property (nonatomic, copy) NSString  *accountVariable;

Or are there any other reason you need to have classobj?

Upvotes: 6

Mrunal
Mrunal

Reputation: 14128

Please check code given below:

AccountViewController.h file:

#import "AccountViewController.h"

@interface BluetoothViewController : UIViewController <GKPeerPickerControllerDelegate, GKSessionDelegate>{

    AccountViewController   *classobj;
}

@property(nonatomic, retain) AccountViewController *classObj;

BluetoothViewController.h file:

#import "AccountViewController.h"

@interface BluetoothViewController : UIViewController <GKPeerPickerControllerDelegate, GKSessionDelegate>

@property(nonatomic, retain) AccountViewController *classObj;

AccountViewController.m file:

 - (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:

- (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.

Upvotes: 0

Related Questions