How to pass a button between two views? - ios

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.

Related

Present different outlets on the same view controller through navigation controller?

I want to present different outlets to the user for different stages of a registration form. For example they enter their first name and last name, press continue and then go onto the next part of the form to enter their date of birth etc and at each stage of the form I want it to be part of a navigation view controller so they can easily press back to go back to the previous part and perhaps change it. Just like how snapchat does when you sign up.
I want only one view controller which will manage all the validation and outlets from the various parts of the form.
I tried creating multiple of the same view controller, and putting different text fields and buttons etc on each page. They were then all embedded in a navigation view controller with segues to each part of the form. The problem with using the same view controller is every time viewDidLoad is called it re-initialises the outlets so ones the user previously entered become nil. I dont want to just save the textfields to a struct for example because the user can press back to go back to this field (and i dont want them to then have to re-enter the information). There must be a simple way to do this?
Also I dont want to create a different view controller for every part because this seems wasteful as not much is displayed on each part

Change view controller cause reset changes

I have two view controller. On the Second controller I have a button which cause a UILabel to change from "larry" to the string "happy". If I press back, which take me to first view controller, and then forward, which take me to the second view controller (from the first view controller). I found that the UILabel change back to default, "larry". How do I make it so that when I change view controller, the changes and data doesn't get clear away.
Recap: How to make it so that every change in a view controller doesn't change back to default after changing to another view controller.
This doesn't only happen with UILable, it also happen with
self.loadPage.hidden = YES;
I also found out what I made to turn 'hidden' also appear back after I pressed back and forward.
Thank
The problem that you are facing is due to the fact that when you press the button a new instance of the second viewcontroller is created and that old one is destroyed.
If you want to persist the data while pressing the buttons, you need some way to store the information somewhere
there are some methods to got about it
You can use instance variables in both the classes to store the data temporarily and display them. You need to know the concept of protocols and delegates for this as well.
You can store the information in NSUserDefaults for some moment so that you can access it later on.
The issue is each time when you goes from first view controller to second, you are initialising a new instance of second view controller which brings back every values you changed to default values.
You need to save the values elsewhere and assign those values whenever control is switched from one view controller to another

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.

Send data and Change Page on button press using UIPageControl

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.

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