1

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.

1
  • Found an interesting thread on here, that suggests two solutions. One, is to create a delegate and pass a notification, and another is to create a field on your child-class (BluetoothViewController) like what Mikael is suggesting below. iphonedevsdk.com/forum/iphone-sdk-development/… Commented Jan 23, 2013 at 15:45

3 Answers 3

6

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?

Sign up to request clarification or add additional context in comments.

4 Comments

Wow thank you. This worked perfectly. Is this how you would transfers all your variables from one class to another class?
That's one of the ways to do it. Or you could send in a variable as parameter. For instance if you create your own init-method, like -(id) initWithAVariable:(NSString*)aString . Maybe you should read up on the MVC pattern.
Although the answer you provided works, there is a problem if you want to transfer a variable to another view controller that you will use later in the program. For example, the next view controller that I will use will be the BluetoothViewController, and the one following that will be ConfirmationViewController. Now, I could pass the variable to the BTVC, as its the next screen I will display and this will work. But if I transfer a variable to CVC, it will not display the variable when I show that controller later in the program. Do you know why this is the case?
But if you do it the same way. Add an instance variable to your CVC. So when you initiate CVC you can set it's variable. Think of it this way: The object (class) that initiates another object will own that object, thus can set it's variables. However, try to do it the MVC way, which is a class that acts as a Model (holding the data) and have that class sending variables or have other objects asking for variables in that class.
0

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.

1 Comment

I'm sorry, but I am confused on what is going on here.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.