Send data and Change Page on button press using UIPageControl - ios

I am using UIPageControl and UIScrollView to move between the views by scrolling.
I have followed this tutorial.
I have a button in the first of the view controllers.
I would like to send data to the second view controller and also change the page. I would like to know how it is done.

You would need to have all of the view controllers created (born at least the first 2) rather than loading purely on demand (scroll).
Add a property to the first controller which holds a reference to the second controller. Set the reference when the instances are created.
When the button is tapped, use the reference to call a method on the second controller.

Related

How to automatically update a label on a viewcontroller from another viewcontroller while using a UIpageviewcontroller to navigate pages

I am currently using a UIPageviewcontroller to navigate between my 3 main viewcontrollers. I have a global variable that I set on the first page using a stepper. The value of that global variable is then used as the value for a label on the second page.
This works, but it requires a button on the 2nd page to update the label. The exception being when I first run the app and change the stepper on the first page before swiping to the second page - as I have the label being updated in the "override func viewdidload()"
I dont want to use a button to update my label on page 2. I would like to be able swipe to page 1, change the stepper, then when I swipe back to page 2, the label on page 2 is automatically updated. (repeatedly)
How do I do this?
Is there a way to kill view controllers within the UIpageviewcontroller? or is there a way to get the "override fun viewdidload()" function to run every time I swipe to a page?
The simple way to do this would be to update the label on page 2 in it's viewWillAppear method. That will get called each time you go back to your page 2 view controller.
Globals are not a great way to set up your data handling however.
You'd be better off making page 2's view controller the delegate on the first view controller. When the users changes the stepper on the first view controller, it could call a "valueChanged()` method on it's delegate.
You should read up on the delegate design pattern to figure out how to do this. It's VERY well documented, and is one of the most common patterns you will find in iOS development.

Accessing function from inside of a subview controller

I am using a walkthrough library written in swift, and I am trying to have the keyboard to be displayed only on a specific step in the walkthrough. If I add a textField to that specific view controller in the storyboard, and make it become the first responder, the keyboard is shown for all of the other view controllers as well. What I am trying to accomplish is when the user is on this specific page, I would like the keyboard to be present, and when they scroll away, for it to disappear with the swipe.
The library offers a function called walkthroughPageDidChange, however it’s on the master view controller where all the subview controllers are attached to. I can check if the pageNumber is the one I want it to be, however I am not sure how to accomplish accessing a function inside of one of the attached view controller pages.
I would like to access the keyboard controller with the following functions:
KeyboardViewController.showKeyboard() - becomeFirstResponser for textField
keyboardViewController.hideKeyboard() - resignFirstResponder for textField
if pageNumber == 2 {
KeyboardViewController.showKeyboard()
}else{
KeyboardViewController.hideKeyboard()
}
How would I accomplish this? How would I be able to access this function inside of another view controller?
Thank you in advance.
You should keep an array of your ViewControllers in the parent ViewController when you create them. So in your array you pick the right controller and call the function you want. I did not see the code where the viewcontrollers are stored in an array your link, but you can just add it where you create the VCs.

How to pass a button between two views?

Is it possible to pass a button between two views?
I mean, when the button is clicked it has to call another view and stay while the view behind it pushesToSegue away.
Are you trying to reuse the same button in multiple view controllers? That is not really how things are done normally in iOS.
I suppose you want to pass some data attached to the Button (i.e. its label or tag) to the next view controller. In that case, you would want to pass the data as properties (e.g. NSString, ...etc.). In your next view controller, create the same button in the same position in the storyboard. Then you can set its label from the data you have passed in.
Now if you really want to pass a UIButton to the next view controller, you can still pass it as a property. However, when someone taps the button, the action method that is called is still the one in your first view controller. You could remap it to another action method in the next view controller, but going down this path is against the general rule of thumb.

Calling multiple views from Uipopovercontroller list view contents

How can I call individual view controllers when I tap each row from my Pop Over List View in the most effective way? Meaning, I don't have to recode to build the Pop Over List View component in each of the individual view controllers, they can sort of share it like a navigation tool.
What I Have Built
I have built the pop over view controller list on my Main View Controller.
This is the 1st view that gets loaded when appDidFinishLaunching is executed.
So, when we run the app, this is what we get:
Link to screenshot of the Pop Over List View:
http://s14.postimage.org/63k567vtd/image.png
For each row in the above Pop Over list, I have a method where I can put in the codes to:
1. Identify which row was selected
2. Based on the row selected, do any action, like NSLog and stuff
3. All this codes sit in the Main View Controller
The Requirement
I am building a demo app, so all data is dummy data at the moment.
I need to call individual View Controller when each of the row in the Pop Over list is tapped.
Each of the View Controllers, will have their own set of data, own set of UI objects, its own XIB file.
So, if user taps on Applications, it must bring up the Applications XIB file and its functionality, followed by any navigations from here. If Application xib has a button that launches another view, this functionality should be in place too.
However, they all must have the same top Navigation Bar with the Pop Over list button, so user can tap that button and see the Pop Over list anytime in the app.
Please share your view.
Thank you.
I suggest looking in to UISplitViewController. It is a component built by Apple to manage what it looks like you are trying to accomplish. Essentially, you set it up with 2 view controllers; the first (called the "master"), would be your table view. The second (called "detail") would be the view controller that gets switched out.
Once you explore it a little and are comfortable with the terminology, here's the general advice:
Inside the master table view controller's didSelectRowAtIndexPath: method, instantiate the appropriate view controller (based on the indexPath), and set it as the detail view controller, like this:
- (void)tableView:tableView didSelectRowAtIndexPath:indexPath {
UIViewController *newDetailVC = // make and configure a new VC based on indexPath
self.splitViewController.viewControllers = [NSArray arrayWithObjects: self, newDetailVC, nil]];
}
Here are the appropriate links:
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UISplitViewController_class/Reference/Reference.html#//apple_ref/occ/cl/UISplitViewController
and further (the later sections of): http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html%23

Dismissing a view makes losing it all values... viewWillAppear as a cure?

I have a basic modal view system.
My app loads the UI base in which there are 2 buttons presenting 2 other views.
In those views, a dismiss button.
Everything works fine.
BUT, in one of the 2 modal views, I have a bunch of UISlider & UISwitch.
I want them to retain their values but the dismiss loses them: as soon as I trigger the button to show the view containing the UI elements, this view is shown with all values for all elements as I put initially in the xib.
should I store all values in variables, then in viewWillAppear I could "recall" them ?
would you advice me another strategy ?
Yes, your proposed approach is exactly the right sort of thing. But be careful; viewWillAppear can be called for many reasons; make sure you're only doing this when the view controller is coming into existence and showing the view for the first time.
NSUserDefaults can be an excellent place to store globally needed info like this. In viewWillDisappear, store the desired state info (values of the sliders and switches) in defaults. Then retrieve them the next time the view is about to appear.
When you create the modal view you are creating a new instance of the modalViewController an the modalView. This new instance knows nothing about any other instance. There are a few ways you can retain the information from previous iterations of these modal view controllers.
How I would do it:
Set up place holders in your main view and pass the values that the user selects back to the main view via a protocol and delegate setup. Then when you segue to the modal view you can load those variables in before displaying the modal view.
So let's say you have a dictionary with all of the values: {slider = YES, someValue=10,...} Create that dictionary in the main view controller, the first one that opens, and place some default values in it.
In your modal view controllers create the same dictionary as a property.
Create a protocol in your modal view controller with a method that is something like
- (void) doneEditing:(NSDictionary *)values
Set up your first view as the delegate for the modal view controller and in the implementation of doneEditing copy the values to the dictionary that is present in the first view before popping the modal view.
When the first view is ready to present the modal view again, copy the values to the dictionary property of the modal view before presenting it.
I hope this gets you headed in the right direction. It's important to remember that each time you segue or create and present a modal view you are creating a brand new instance of that view, it knows nothing about the previous instance at all unless you tell it something about it.

Resources