ios 如何从Objective C中的另一个类正确访问变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14483206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to correctly access a variable from another class in Objective C
提问by Sulaiman Majeed
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"
我查看了各种论坛,试图找到解决此问题的方法。我目前有 2 个类,BluetoothViewController 和 AccountViewController。我想要做的是将用户从 AccountViewController 中选择的帐户传递给 BluetoothViewController,以便我稍后可以在 BluetoothViewController 中使用该字符串。我所做的是在 BluetoothViewController.h 中创建 AccountViewController 的一个实例,并为 AccountViewController.h 文件中的字符串创建了一个属性。我试图访问的字符串是“account8”
My AccountViewController.h file:
我的 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;
My AccountViewController.m file:
我的 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];
}
My BluetoothViewController.h
我的 BluetoothViewController.h
#import "AccountViewController.h"
@interface BluetoothViewController : UIViewController <GKPeerPickerControllerDelegate, GKSessionDelegate>{
AccountViewController *classobj;
}
@property(nonatomic, retain) AccountViewController *classObj;
My BluetoothViewController.m file:
我的 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;
}
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.
当用户从表中选择行时,内容将保存在变量 account8 中。然后将调用 BluetoothViewController。现在,当 BluetoothViewController 加载时,应该显示用户选择的帐户。但是标签返回空白。我想知道为什么它不能正确访问 AccountViewController 中保存的变量。
回答by Mikael
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:
您可以做的是在 Bluetoothcontroller 中创建一个实例变量,然后在实例化该对象后从 AccountViewController 设置它。例如:
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
?
或者还有什么其他原因需要你classobj
?
回答by Mrunal
Please check code given below:
请检查下面给出的代码:
AccountViewController.h file:
AccountViewController.h 文件:
#import "AccountViewController.h"
@interface BluetoothViewController : UIViewController <GKPeerPickerControllerDelegate, GKSessionDelegate>{
AccountViewController *classobj;
}
@property(nonatomic, retain) AccountViewController *classObj;
BluetoothViewController.h file:
BluetoothViewController.h 文件:
#import "AccountViewController.h"
@interface BluetoothViewController : UIViewController <GKPeerPickerControllerDelegate, GKSessionDelegate>
@property(nonatomic, retain) AccountViewController *classObj;
AccountViewController.m file:
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 file:
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
}
Hope this will help you.
希望这会帮助你。
回答by Darius Miliauskas
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:
使用您的 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