Accessing function from inside of a subview controller - ios

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.

Related

How to tell an UIView about an event in a collectionviewcell?

Reference Image
Hello!
I'm making a Swift project using the MVC pattern, and I'm hoping you guys could help me with how to best pass information between different classes.
I'll do my best to describe, but please take a look at my badly drawn reference for some additional clarity.
The project is build with a Model, that's a simple class with the rules of the project, a View Controller, that has an instance of both the View and the Model, passes information between them and initiates the View.
The View has all the element variables, and sets the layout. The view has a CollectionViewController, that in turn has the CollectionViewCells. One of those cell has a Button. There's of course some other classes, but they're not relevant.
What I'm trying to accomplish is that I want the user to click the Button in the CollectionViewCell. The CollectionViewCell should then tell the View that the Button was pressed, and the View should take some Information and send to the ViewController, that will interpret it for the Model.
More or less Button > User Clicks -> (CollectionViewController) -> View > View collects Information -> CollectionView -> Model.
I cannot delegate the buttonclick event to the View, as UIViews cannot be delegated to. I can use a Closure to send the buttonclick event to the CollectionViewController, but I don't seem to be able to send it further from there.
So the two alternatives I'm looking at is creating a NotificationCenter just to tell the View a button has been pressed, or making the View a ViewController too. Neither seems ideal. Does anyone have a better solution, or any insight on what I should do here?
One solution based on your reference image can be this:
you can send your click event with clouser to your collectionView parent view and in your view have a weak instance of your parent viewController. when the clouser called, you can call the method from your parent instance and update your model

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.

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.

How to find the name of the view controller beneath a popover ios7

This is probably a very simple question but I can't find the answer to it.
I am working on a new project that uses Storyboards for the first time.
I have a number of view controllers that connect the way I want them to.
Each view controller has an info button.
I have one view controller (AboutViewController) that I want to use to display the info for all the view controllers. I am currently calling this via a popover segue from each screen. So I have one destination view controller (AVC) that I am calling from a number of VCs- VC1toAVC, VC2toAVC, VC3toAVC etc. I want two textfields in AVC to change, depending on which VC called it.
So here's the problem- how can I tell which view controller called the popup? It's basically the view that's below the popover. Currently I'm storing it as a variable but that's not ideal. I'm guessing it has something to do with the segue identifiers?
Any and all help much appreciated!
One approach to this is adding a property to your pop up view controller and then define the
prepareForSegue:sender:
method so you set your destination view controller's property to the sender of the segue.

How make static view on any viewcontroller in Xcode?

I Have interesting question.
I have two screens, I need that would be the third screen elements (buttons, label) are static and do not change when you move from one screen to another. .
So MAIN DISPLAY 1 will be change, DISPLAY 2 too , but button and others(label for example) need be static.
Big Thanks for all help, sorry i don't have any code with this problem. Because i don't know how this make...
Ok, so if you have a UI that won't change it's content you have to consider the following:
If the user can navigate from one screen to another, then you will have to create multiple view controllers(it can be done in other ways but this is the easiest and best way to doit) that will have their own UI, that can be easily created in storyboards like this:
Create a new project that use storyboars (you can use the Xcode templates that support your needs)
In your generated storyboard, drag and drop a new UINavigationController, and in the Attributes inspector check the Is initial view controller
Drag another UIViewController into your storyboard, select your navigation controller, hold right click and drag to the newly added view controller, a popup will appear and from that popup select root view controller. Now the newly added view controller will be the first view that the app will display. On this view controller add your UI elements (buttons, labels, etc). If you want another screen to be shown when a user taps on a button, drag another view controller and select the button from which you want to display the next screen, hold right click drag to the newly added view controller, from the popup select push. Now when the user tap on the button the next view controller will be shown and as a bonus because you use iOS, the system will create a back button so that you can go back to your first view controller.
Ok, this is the a basic tutorial from which you can start and expand, but for that you will have to read more, spend nights on google/SO to find solutions, you will learn apple docs by heart and other things that are required so that you can be a great iOS developer.
My the iOS force be with you and Steve Jobs watch your steps.
This is off the top of my head:
you could put a view into your uiwindow and place your static elements there, and then give a transparent background to display1 and display2. then you can have the two displays forward their touches to the background elements to have actions on the buttons. im sorry i dont have any code, but i never tried that ;)
A much better way is to instantiate your ViewController like so:
this.NavigationController.PresentModalViewController (StaticViewController.staticViewController, true);
Then inside your instantiated View Controller you setup a static variable that holds that instance of the View Controller:
public static StaticViewController staticViewController;
When the Static View Controller fires up for the first time:
staticViewController = this;
Now in the future when you fire up the Static View Controller (from wherever in your app) you can check to see if the StaticViewController.staticViewController variable is null or not.
Use simple if else logic to load it up accordingly:
if (StaticViewController.staticViewController == null) {
StaticViewController.staticViewController staticViewController = this.Storyboard.InstantiateViewController ("StaticViewController") as StaticViewController;
this.NavigationController.PresentModalViewController (StaticViewController.staticViewController, true);
} else {
if(!StaticViewController.staticViewController.IsBeingPresented) //safeguard against click happy users
this.NavigationController.PresentModalViewController (StaticViewController.staticViewController, true);
}
You should add the static views to your UINavigationController or whatever controller you are using for managing multiple controllers.

Resources