如何从iOS中其他viewcontroller的textview获取NSDictionary中的值

如何从iOS中其他viewcontroller的textview获取NSDictionary中的值,ios,objective-c,nsdictionary,Ios,Objective C,Nsdictionary,我有一个登录屏幕,用户在其中输入电子邮件ID。当用户登录并转到其他视图控制器时,我需要将电子邮件ID传递给我在另一个视图控制器中提到的NSDictionary对象 NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys: @"abc@xyz.com", @"EmailID", @"http://www.yahoo.com", @"URL", @"344", @"Phone", @"", @"UserID", ni

我有一个登录屏幕,用户在其中输入电子邮件ID。当用户登录并转到其他视图控制器时,我需要将电子邮件ID传递给我在另一个视图控制器中提到的
NSDictionary
对象

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys: @"abc@xyz.com", @"EmailID", @"http://www.yahoo.com", @"URL", @"344", @"Phone", @"", @"UserID", nil];
如果我替换
@”abc@xyz.com“
到[
NSString stringWithFormat:@“@”,firstViewControllerObject.emailIdTextField.text]
并尝试在日志中获取输出,我得到一个空值

如何将用户输入的电子邮件id传递到
NSDictionary
对象并在日志中获取值


使用Xcode 4.6,我不使用故事板。在我的项目中使用
XIB

我认为问题在于格式字符串。我想你忘了带“%”号了。请尝试以下操作:

[NSString stringWithFormat:@"%@", firstViewControllerObject.emailIdTextField.text]

希望这有帮助

对于其他视图控制器(我们称之为
otherViewController
)要访问该字段,
otherViewController
需要“拥有”
firstViewControllerObject
对象。也就是说,
firstViewControllerObject
对象需要从
otherViewController
中实例化。
就应用程序中的顺序而言,您的
otherViewController
应该首先实例化,并且在其中
firstViewControllerObject
应该实例化并显示
presentViewController:animated:completion:

正如我在评论中所说的,使用
[NSString stringWithFormat]
当参数为“%@”时,因为它不做任何操作,所以只需执行以下操作:

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
    firstViewControllerObject.emailIdTextField.text ... ];

似乎
anotherViewController
逻辑上需要email属性,而
firstViewController
拥有
anotherViewController

因此,我认为您可以将
另一个viewController
设置为一个名为
email
的属性,让
fistViewController
在创建
viewController时将
email
传递给它

假设您的两个视图是
FirstViewController
SecondViewController

  • 只需在
    SecondViewController
    中定义一个名为
    emailID
    的属性,当用户登录时,您正在启动
    SecondViewController
    ,在电子邮件Id
    textField
    中使用字符串值设置
    emailID
    属性

  • 您还可以编写自己的
    协议
    ,以实现
    授权
    设置电子邮件


  • 您是如何分配/启动firstViewControllerObject的?
    stringWithFormat:@“
    ?您的意思是
    stringWithFormat:@“%@”
    ?但是您是否考虑过这样一个事实:您没有格式化任何内容,并且该语句与简单地指定参数相同;即:replace@”abc@xyz.com"使用
    firstViewControllerObject.emailIdTextField.text
    。在执行任何操作之前,请执行
    NSLog(@“%@”,firstViewControllerObject.emailIdTextField.text);
    如果打印值!oops…我刚刚看到了注释。@如果删除[nsstring stringwithformat..,我将使用NSLog获取值…仍然获取(null)并直接访问对象。这是正确的;stringWithFormat是多余的。有关对象所有权的更广泛问题,请查看我的答案。如果我按照您的建议删除[nsstring stringWithFormat]并访问文本字段,则仍然在nslog中获取(null)。如果我不使用[nsstring stringWithFormat..],键值对被打乱,我没有按预期获得键值对