簡體   English   中英

如何從iOS中其他viewcontroller的textview獲取NSDictionary的值

[英]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

我心里有兩個解決方案給你......

假設您的兩個視圖是FirstViewControllerSecondViewController

  1. 只需在SecondViewController定義一個名為emailID的屬性,當用戶登錄時,當你啟動SecondViewController時,使用Email Id textField的字符串值設置emailID屬性。

  2. 您也可以編寫自己的protocol來實現Delegation設置電子郵件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM