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