我看过各种论坛,试图找到解决此问题的方法。我目前有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];
}
我的BluetoothViewController.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中保存的变量。
最佳答案
您可以做的是在Bluetoothcontroller中创建一个实例变量,然后在实例化该对象后从AccountViewController对其进行设置。例如:
AccountViewController:
BluetoothViewController *bluetoothViewController = [[BluetoothViewController alloc] initWithNibName:@"BluetoothViewController" bundle:nil];
bluetoothViewController.accountVariable = _account8;
BluetoothViewController:
@property (nonatomic, copy) NSString *accountVariable;
还是有其他原因需要
classobj
?
关于ios - 如何从Objective C中的另一个类正确访问变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14483206/