1

我查看了各种论坛,试图找到解决此问题的方法。我目前有 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];

}

我的蓝牙视图控制器.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 中的变量。

4

3 回答 3

6

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

帐户视图控制器:

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

蓝牙视图控制器:

@property (nonatomic, copy) NSString  *accountVariable;

或者还有其他理由需要classobj吗?

于 2013-01-23T15:39:00.857 回答
0

请检查下面给出的代码:

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
}

希望这会帮助你。

于 2013-01-23T15:54:16.907 回答
0

使用您的 XCode,您需要进行导入,通过将其声明为属性来创建对象,然后使用“object.variable”语法。文件“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
于 2014-11-12T03:28:23.567 回答