[英]How to get value in NSDictionary from the textview of other viewcontroller in iOS
我有一個登錄屏幕,用戶輸入電子郵件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]
希望這可以幫助!
正如我在評論中所說,當參數為"%@"
時,使用[NSString stringWithFormat]
是沒有意義的,因為它什么都不做,所以只需這樣做:
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
firstViewControllerObject.emailIdTextField.text ... ];
對於你的其他視圖控制器(讓我們稱之為otherViewController
)來訪問該字段, otherViewController
需要“擁有” firstViewControllerObject
對象。 即, firstViewControllerObject
對象需要在otherViewController
實例化。 就你的應用程序中的事物順序而言,你的otherViewController
應該首先被實例化,並且在其中firstViewControllerObject
應該被實例化並呈現presentViewController:animated:completion:
.
看來, anotherViewController
邏輯上需要的電子郵件屬性和firstViewController
擁有anotherViewController
。 所以我覺得你可以做anotherViewController
命名屬性email
,並讓fistViewController
通過email
給它而創造viewController
。
我心里有兩個解決方案給你......
假設您的兩個視圖是FirstViewController
和SecondViewController
。
只需在SecondViewController
定義一個名為emailID
的屬性,當用戶登錄時,當你啟動SecondViewController
時,使用Email Id textField
的字符串值設置emailID
屬性。
您也可以編寫自己的protocol
來實現Delegation
設置電子郵件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.