简体   繁体   中英

Getting UITextField value in UIPageViewController

It's my first question here so please bear with me, (I was searching for days but I can't find any answer! TT) I am also a newbie on IOS development, so sorry if I get the terms wrong, I need to get the value of UITextField in my app. I am using UIPageViewController and 2 View Controllers:

The first View Controller is the root view using UIPageViewControllerDataSource , and this is where my UIPageViewController declared. The second view is the pages view, inside this view is there are 2 UITextFields. So in my app, I have 2 pages both using the second view controller.

My first problem is, I was not able to get the UITextFields ' value using the root view class, but then I found a way to do that by declaring UIPageViewController in the .m of my rootviewcontroller interface, so I think I solved it my own way.

However, I am getting this values by clicking a button before turning to next page, Is there anyway I could get the UITextField value when I turn to next page without clicking the button? Next is, when I turn to next page and go back to the first page, when I click the button it is not getting the UITextField value anymore.

So, I was able to get UITextField value by identifying the placeholder on the first UITextField of every page.

I have the viewControllerBeforeViewController and viewControllerAfterViewController methods, but their behavior is weird.. the calling of this method at my app is inaccurate when I turn my page back and forth for many times sometimes one of this method is not called.

Also, I used this site http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/ as my guide on creating this.

Hope somebody could help me… Thanks!

You can use NSUserDefaults to hold the value of your TextField.

let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject("TextField value", forKey: "nameKey")

and to get the value

let name = defaults.stringForKey("nameKey")

Once you save the value, you can use this in the whole project.

UIPageViewController has the viewControllers property. You can access it to get an array of the UIViewController s. Using this, you will be able to get the access the SecondViewController s.

pageViewController.viewControllers[1] will give you the instance of the first SecondViewController . Just make sure to declare the textField globally in the SecondViewController and you can access it to retrieve the text entered.

Also, Paddy's answer should work as well. Have two different keys for the two textField s and access them separately.

Alright! I got it all solved by myself and the help of these people above! Thanks for the advise guys! you were such a great help to me!!! What I did is just I made two viewcontroller for the pages so I don't need my UItextfield to be sharing two inputs! Thanks!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM