如何从iOS中其他viewcontroller的textview获取NSDictionary中的值
我有一个登录屏幕,用户在其中输入电子邮件ID。当用户登录并转到其他视图控制器时,我需要将电子邮件ID传递给我在另一个视图控制器中提到的如何从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
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
,在电子邮件IdtextField
中使用字符串值设置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..],键值对被打乱,我没有按预期获得键值对