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
Objective c 对象值不在其他类中_Objective C_Ios - Fatal编程技术网

Objective c 对象值不在其他类中

Objective c 对象值不在其他类中,objective-c,ios,Objective C,Ios,我正在创建一个简单的基于iPhone导航的应用程序。在这方面,我将从头等舱转到二等舱。在第二个类中,我将为字符串(selectedName)设置一些值。当我返回到(void)视图中的第一个类时,将显示,我正在通过该类的对象访问selectedString的值。我已经在-(void)ViewDidLoad方法的第一个类中分配了第二个类的对象,但是它在那里记录了null值。请帮帮我 在第二个类中,我有一个名为selectedString的字符串,我正在为它设置一些值,并记录它,它记录得很好 在第一类

我正在创建一个简单的基于iPhone导航的应用程序。在这方面,我将从头等舱转到二等舱。在第二个类中,我将为
字符串(selectedName)
设置一些值。当我返回到
(void)视图中的第一个类时,将显示
,我正在通过该类的对象访问
selectedString
的值。我已经在-
(void)ViewDidLoad
方法的第一个类中分配了第二个类的对象,但是它在那里记录了
null
值。请帮帮我

在第二个类中,我有一个名为selectedString的字符串,我正在为它设置一些值,并记录它,它记录得很好

在第一类viewDidLoad方法中,我像这样分配了第二类的对象

  Second *obj=[Second alloc]initWithNibName:@"Second" bundle:nil];
在视图中,我将以如下方式访问selectedString,但它记录为null

 NSLog(@"%@",obj.selectedString);                              

请尝试将数据传回:

要将数据从ViewControllerB传回ViewControllerA,您需要使用协议和委托

为此,我们将使ViewControllerA成为ViewControllerB的代表,这允许ViewControllerB将消息发送回ViewControllerA,使我们能够将数据发送回

VIEWCONTROLERA作为ViewControllerB的代表,必须符合ViewControllerB的协议,我们必须指定该协议。这将告诉ViewControllerA必须实现哪些方法

1) 在#import下面但在@interface上面的ViewControllerB.h中指定协议

@protocol ViewControllerBDelegate <NSObject>
- (void)addItemViewController:(ViewControllerB *)controller didFinishEnteringItem:(NSString *)item;
@end
#import "ViewControllerB.h"

    @interface ViewControllerA : UIViewController <ViewControllerBDelegate>
4) 这就是现在位于ViewControllerA中的ViewControllerB。h告诉ViewControllerA导入ViewControllerB并遵守其协议

@protocol ViewControllerBDelegate <NSObject>
- (void)addItemViewController:(ViewControllerB *)controller didFinishEnteringItem:(NSString *)item;
@end
#import "ViewControllerB.h"

    @interface ViewControllerA : UIViewController <ViewControllerBDelegate>
6) 我们需要做的最后一件事是在将ViewControllerB推到导航堆栈之前告诉ViewControllerB ViewControllerA是它的代理

ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNib=@"ViewControllerB" bundle=nil];
viewControllerB.delegate = self
[self pushViewController:viewControllerB animated:YES];

你为实现它写了什么,随此说明添加一些代码。您希望通过引用而不是通过值传递:我已使用所需的代码编辑了问题。这很好,但请告诉我一件事,为什么我无法通过创建对象来完成此操作……感谢您的回复..您实现的方法可用于将数据从另一个视图控制器,您可能正在将其推送到导航堆栈。在导航堆栈上传递值时,您必须使用委托和协议
ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNib=@"ViewControllerB" bundle=nil];
viewControllerB.delegate = self
[self pushViewController:viewControllerB animated:YES];