Accessing ParentViewController from UITextFields inside a custom View

By : Marko
Source: Stackoverflow.com
Question!

I have a UIScrollView to which I append multiple custom UIView's that contain UITextFields...

In each of these text fields I handle the EditingChanged event so that I do some calculations and append them to a UILabel...

Now... Outside my UIScrollView I have a TextBox who's value I need to access from the TextFields inside my custom View...

Can I scroll up the hierarchy and find that control by it's tag somehow? I've tried using ParentViewController but that's always null.

Any help appreciated

By : Marko


Answers

Is the TextBox outside the UIScrollView in the same UIViewController? You should be using a controller to manage the communication. Handle the EditingChanged in the lowest level that contains both text boxes. So, if they are in the same UIViewController, have it handle the EditingChanged and have a reference to the text box as well.



Or you can use UIApplicationDelegate.

  1. Declare an instance referring to UISCrollView, and set UIScrollView to the instance.

    @interface YourAppDelegate { YourUIScrollView* yourScrollView; } @property (nonatomic, retain) YourUIScrollView* yourScrollView; @end

  2. Wherever you want to access the scrollview, you can do as below,

    YouUIScrollView* yourScrollView = ((YourAppDelegate*)[[UIApplication sharedapplication] delegate]).yourScrollView;

By : alones


You could go the other way and go down the hierarchy to get the value from the lower UITextField and perform the calculation outside of the UIScrollView. A way you could access the lower UITextField's is as follows:

foreach (scrollView.ViewWithTag(tagNum).SubViews.OfType
By : Luke


This video can help you solving your question :)
By: admin