Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 访问其他类(组合导航和选项卡控制器)中的变量时为空值_Iphone_Objective C_Ios_Xcode - Fatal编程技术网

Iphone 访问其他类(组合导航和选项卡控制器)中的变量时为空值

Iphone 访问其他类(组合导航和选项卡控制器)中的变量时为空值,iphone,objective-c,ios,xcode,Iphone,Objective C,Ios,Xcode,我有一个位于选项卡栏控制器内的导航控制器,每当我试图从导航控制器内的类访问一个类时,我的所有值都返回(null) 这就是我试图做到的 AppDelegate.h @interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate> { NSString *searchQueryA; } @property (strong, nonatomic) NSString *searc

我有一个位于选项卡栏控制器内的导航控制器,每当我试图从导航控制器内的类访问一个类时,我的所有值都返回(null)

这就是我试图做到的

AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate> {
NSString *searchQueryA;
}

@property (strong, nonatomic) NSString *searchQueryA;
第三个虚拟控制器

- (IBAction)showDetail:(id)sender {
AppDelegate *appDelegate = [[AppDelegate alloc] init];
appDelegate.searchQueryA = _searchField.text;
masterViewController = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil];
[self.navigationController pushViewController:masterViewController animated:YES];
}
 AppDelegate *appDelegate = [[AppDelegate alloc] init];
 searchQueryM = appDelegate.searchQueryA;

 NSLog(@"%@", searchQueryM);
@synthesize searchQueryM;
 @synthesize searchField = _searchField;

 ...

 - (IBAction)showDetail:(id)sender {
 _code_
 NSLog(@"%@", searchField.text);
 _code_
MasterViewController.h

#import "MasterViewController.h"
#import "AppDelegate.h"

@class MasterViewController;

@interface ThirdViewController : UIViewController {
code
}

@property (strong, retain) MasterViewController *masterViewController;
#import "AppDelegate.h"
@interface MasterViewController : UITableViewController
{
NSString *searchQueryM;
}

@property (nonatomic, strong) NSString *searchQueryM;
@interface MasterViewController : UITableViewController
{
NSString *searchQueryM;
}

@property (nonatomic, strong) NSString *searchQueryM;
@interface ThirdViewController : UIViewController {
UITextField *_searchField;
}

@property (nonatomic, strong) IBOutlet UITextField *searchField;
MasterViewController.m

- (IBAction)showDetail:(id)sender {
AppDelegate *appDelegate = [[AppDelegate alloc] init];
appDelegate.searchQueryA = _searchField.text;
masterViewController = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil];
[self.navigationController pushViewController:masterViewController animated:YES];
}
 AppDelegate *appDelegate = [[AppDelegate alloc] init];
 searchQueryM = appDelegate.searchQueryA;

 NSLog(@"%@", searchQueryM);
@synthesize searchQueryM;
 @synthesize searchField = _searchField;

 ...

 - (IBAction)showDetail:(id)sender {
 _code_
 NSLog(@"%@", searchField.text);
 _code_
在日志中我可以看到searchQueryM是(null)。如果我尝试从另一个类访问AppDelegate中的变量,导航控制器不涉及这个类,那么它显示的非常好。我错过了什么

如果您需要查看更多代码,我很乐意提供

编辑:

为了便于阅读,我将在此处发布代码更改:

我的AppDelegate.h中有委托

@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate> {
NSString *searchQueryA;
}

@property (strong, nonatomic) NSString *searchQueryA;
正如莱昂纳多指出的,我只分配并初始化了我的AppDelegate。我将该片段更改为:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
searchQueryM = appDelegate.searchQueryA; 
但是仍然没有,因为searchQueryM仍然是(null)

这就是我对searchQueryM所做的

MasterViewController.h

#import "MasterViewController.h"
#import "AppDelegate.h"

@class MasterViewController;

@interface ThirdViewController : UIViewController {
code
}

@property (strong, retain) MasterViewController *masterViewController;
#import "AppDelegate.h"
@interface MasterViewController : UITableViewController
{
NSString *searchQueryM;
}

@property (nonatomic, strong) NSString *searchQueryM;
@interface MasterViewController : UITableViewController
{
NSString *searchQueryM;
}

@property (nonatomic, strong) NSString *searchQueryM;
@interface ThirdViewController : UIViewController {
UITextField *_searchField;
}

@property (nonatomic, strong) IBOutlet UITextField *searchField;
MasterViewController.m

- (IBAction)showDetail:(id)sender {
AppDelegate *appDelegate = [[AppDelegate alloc] init];
appDelegate.searchQueryA = _searchField.text;
masterViewController = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil];
[self.navigationController pushViewController:masterViewController animated:YES];
}
 AppDelegate *appDelegate = [[AppDelegate alloc] init];
 searchQueryM = appDelegate.searchQueryA;

 NSLog(@"%@", searchQueryM);
@synthesize searchQueryM;
 @synthesize searchField = _searchField;

 ...

 - (IBAction)showDetail:(id)sender {
 _code_
 NSLog(@"%@", searchField.text);
 _code_
我对Objective-C(以及OO编程)相当陌生,可能应该读一本关于它的书,但在我看来,没有比这更多的东西了。如果我错了,请纠正我

编辑2

第三个虚拟控制器

#import "MasterViewController.h"
#import "AppDelegate.h"

@class MasterViewController;

@interface ThirdViewController : UIViewController {
code
}

@property (strong, retain) MasterViewController *masterViewController;
#import "AppDelegate.h"
@interface MasterViewController : UITableViewController
{
NSString *searchQueryM;
}

@property (nonatomic, strong) NSString *searchQueryM;
@interface MasterViewController : UITableViewController
{
NSString *searchQueryM;
}

@property (nonatomic, strong) NSString *searchQueryM;
@interface ThirdViewController : UIViewController {
UITextField *_searchField;
}

@property (nonatomic, strong) IBOutlet UITextField *searchField;
第三个虚拟控制器

- (IBAction)showDetail:(id)sender {
AppDelegate *appDelegate = [[AppDelegate alloc] init];
appDelegate.searchQueryA = _searchField.text;
masterViewController = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil];
[self.navigationController pushViewController:masterViewController animated:YES];
}
 AppDelegate *appDelegate = [[AppDelegate alloc] init];
 searchQueryM = appDelegate.searchQueryA;

 NSLog(@"%@", searchQueryM);
@synthesize searchQueryM;
 @synthesize searchField = _searchField;

 ...

 - (IBAction)showDetail:(id)sender {
 _code_
 NSLog(@"%@", searchField.text);
 _code_
如果我在searchField文本字段中键入“asd”,并将其与日志一起输出,我会得到“asd”。
}

为什么要允许您的AppDelegate初始化? 应通过以下方式访问AppDelegate:

[[UIApplication sharedApplication] delegate]

我们应该看看您通常是如何初始化searchQueryM的,您得到的是null,可能是因为AppDelegate只得到分配和初始化,但初始化其属性的逻辑从未被调用。

感谢您的纠正,似乎我把这个问题搞糟了,但仍然没有解决我的问题。AppDelegate*AppDelegate=(AppDelegate*)[[UIApplication sharedApplication]委托];searchQuery=appDelegate.searchQuery;将其更改为上面的代码,仍然获得空值。我的AppDelegate.huhm中有一个,搜索字段是IBOutlet吗?如果是这样,您是否在ThirdViewController中连接了InterfaceBuilder?在showDetail方法中放置断点或Log语句,然后查看从那时起是否获得了正确的值。\u searchField是一个IBOutlet,它已连接,记录后我会获得正确的值。我将在原始帖子中发布_searchField的代码。请注意,您当然从未分配应用程序委派,但即使在ThirdViewController中,您也在使用sharedApplication委派?请记住在所有ViewController和相关类中更改此选项。仅此而已!我忘了把它放在第三台电脑里。我只分配并初始化了它。但奇怪的是,当我在AppDelegate中检查变量的值时,它就是我刚刚更改的值。非常感谢莱昂纳多!