2015-07-12 54 views
1

層次結構:NavigationController(Root) - > SignupViewController(2nd Level) - > SignupTableView(3rd Level)。 SignupViewController包含嵌入SignupTableView的ContainerView。 SignupTableView包含您在圖像中看到的字段。UIContainerView中的UITextField窗體當文本存在時返回nil

我定義

@property (nonatomic,strong) SignupTableViewController *tableViewController and 
    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 
    if([[segue identifier] isEqualToString:@"signupFieldSegue"]){ 

    SignupTableViewController *destination = (SignupTableViewController *)segue.destinationViewController; 
    self.tableViewController = destination; 
    } 

}

SingupTableView的內部。

一種IBAction爲按鈕按壓內的驗證在UIContainerViewUITableView的字段,但是從UITextField S上的SignupTableViewController內返回的值是零。我試圖self.tableViewController.usernameField.text和定義

- (NSString *)getUsername { 
    return _usernameField.text; 
} 

兩種解決方案都返回nil當文字確實存在。任何方式來訪問文本?

Screen Image

+0

「_usernameField」聲明在哪裏? – ndmeiri

+0

是否可以共享項目的最小化版本?我很難理解視圖控制器之間的關係。 – ndmeiri

+0

太棒了!別客氣。我很高興你明白了。 – ndmeiri

回答

0

從你寫你的問題是什麼,好像你的容器視圖股主(父)視圖控制器屏幕。無論如何,您首先需要確保驗證方法在包含您的表單的視圖控制器被釋放之前執行。

在您的父視圖控制器中,您應該保留對segue的目標視圖控制器的引用。這樣,來自父視圖控制器的驗證可以輕鬆地從容器視圖中的視圖控制器中檢索數據。

首先,在你父視圖控制器聲明一個屬性:

@property (nonatomic, weak) FormViewController *formViewController; 

然後,在prepareForSegue:sender:,該屬性設置爲SEGUE的目標視圖控制器。

- (void)prepareForSegue:(nonnull UIStoryboardSegue *)segue sender:(nullable id)sender { 

    if ([segue.identifier isEqualToString:@"signupFieldSegue"]) { 
     FormViewController *destination = (FormViewController *)segue.destinationViewController; 
     self.formViewController = destination; 

     // continue preparing for the segue... 
    } 
} 

然後,在您的驗證方法中,使用該屬性訪問UITextField值。下面是使用您的getUsername方法的例子,假設FormViewControllerusernameField屬性:

- (NSString *)getUsername { 
    return self.formViewController.usernameField.text; 
} 

再次,請記住,如果你試圖訪問其文本字段之前,你的表單視圖控制器被釋放這些都不能正常工作。

+0

編輯我的帖子以提供更多信息...容器視圖內的子視圖仍然可見,如圖所示。我按下Sign Up,並調用IBAction方法。單步執行代碼,使用getUsername()或self.textfield.text訪問任何字段都返回nil。不知道爲什麼。 – jried

+0

@jried你試過我的解決方案嗎? – ndmeiri

+0

是的,我做到了。圖像是仍然存在的錯誤的屏幕截圖。對於tableviewcontroller,唯一不同的是(弱,非原子)vs我原來的(強壯的,非原子的)。 – jried