I am working on ROR app. The thing i need is a simple slider for view and whenever user change slider i want a method to be call with argument as the value in the slider.
My question:
1) How to call a method every time user change slider.
2) Which is the best place to define method i.e in which file controller class or somewhere else.
A slider would be a JavaScript thing. So you'd have JavaScript events listening for slider changes, and then pulling out whatever attributes you need from that event. Hopefully you could reuse a slider plugin which would almost certainly provide such an event hook for you. Check out: http://vandelaydesign.com/blog/tools/awesome-jquery-sliders/.
If you're saying that after the JavaScript event you'd like to pass the slider state back to your server... then you'd need to make an AJAX call. For this, create a controller that makes sense for receiving this call and point the AJAX code to an action in this controller. The controller can then do whatever you want with the value: store it in the database or whatever.
Related
The sample code for UIDocumentBrowserViewController has a comment in documentBrowser:didRequestDocumentCreationWithHandler: that says Optionally, you can present a template chooser before calling the importHandler.
But how? If I instantiate a view and its controller to use for selecting a template, and call presentViewController:animated:completion: on it, the code doesn't wait for the presented view to be dismissed, but continues happily on. So how can I wait for the the user to select a template in the presented view?
I figured it out. The trick was to realize that there is no need to call the importHandler block already in the documentBrowser:didRequestDocumentCreationWithHandler method. You can store the block in an instance variable of the object you use to select a template (in my case, an instance of a class derived from UICollectionViewController), present that dialog, return, and then call the stored importHandler block much later in the suitable method of that template selection class, in my case the collectionView:shouldSelectItemAtIndexPath.
See code here
I have a pure dynamic page page1VC (which is parent) where i have few buttons which takes them to page2VC, page3VC, page4VC
There is next button and back button in every view controller (no back button in p1VC as this is parent)
I can navigate to any viewController from any other vc i.e, from p1VC -> p2VC -> p4VC -> p3Vc
on tap of back in any view controller, that takes me to p1VC
page1VC gets all info to display from API and stores in variable p1APIInfo
On tap of back button from page2VC or page3VC or page4VC --> this should take me to page1VC
My question is:
As my page1VC is pure dynamic, on 1st landing of this page, i get all info from API
When I navigate to page4VC and on tap of back it should take me to page1VC and here i dont want API to be called anytime when user taps back button
As of now i have a dummy variable in page1VC which is copy of p1APIInfo variable (say dummyAPIInfo)
Each time when i tap next im passing that variable to another VC, and on tap of back from that VC im passing back that variable. So basically this variable is to only transport data to other VC and get it back to page1VC on tap of "back" button. Im not using any data in this dummy variable in page2VC/page3VC/page4VC
However this works, but this is not best practice.
I can think of saving my page1 data in userdefaults/singleton but im looking for more optimizing solution
pls suggest how to handle
So to summarize: I want my API call to happen only once i.e., during 1st time landing..after that whenever user visits/tap back button from some other page, i need a way not to call API. It should use same data that user gets during 1st API call.
Hope I'm clear with my question. Pls advice
Make sure you do the calls in a function that isn't called multiple times. For instance, if your api calls are in the viewWillAppear method, you'll do a fetch every time you pop your navigation stack. Putting the api calls in viewDidLoad will ensure that your api calls only happen once.
As for a way to transport data back to page1...
You could create a custom model that page1 relies on, and inject that model to other pages
You could use the delegation pattern to pass back information
You could use notifications and observers to broadcast and catch updates
You could pass a reference of page1 to any other page and allow other pages to make updates to page1 on its behalf
All 4 strategies are viable, and really depends on your use case and what other things you need in your app. You could even use a singleton. UserDefaults seems like a bad idea though, since your use case is to update a model for a view controller.
I am trying to create custom control with directx10/direct2D output (panel, not a form). I do all rendering in the overriding OnPaint method, however I have read somewhere that it is wrong and RenderLoop should be used instead. But where should I insert RenderLoop.Run if I can write code only inside of the control? Thank you.
You would create a thread, and have RenderLoop.Run inside the thread. When you do this you have to make sure that events sent back and forth between the components are invoked in a safe manner.
I am writing code for a tip calculator in Xcode using swift. I am reading a book and was instructed to connect the slider to the calculateTip method. I am trying to figure out what connecting the action will do. Does this simply mean that every time the slider is moved the calculateTip method will be called?
This particular connection means that you assign an action to the slider: In this case your calculateTip method. By default the slider sends a message to this method, when its value has changed. This connection can also be done programmatically, but Xcode makes that easier for you.
So to answer your question: yes. Anytime the slider is moved the calculateTip method is called. But take a look on the sliders continuousproperty to decide whether this method should be called when the user stops dragging the slider, or — as the name suggests — to send continuous messages.
Yes, it depends which action you choose exactly, but if you select the ValueChanged action then yes, it will call that method every time the slider moves.
While creating a new IBAction Method, I have dragged from the Button in the storyboard to my header file as I should. The Popup that appears I have noticed has an arguments dropdown which offers 3 options which are none, sender and sender and event. What is the difference between 'none' and 'sender' and in what situations would each be used?
None
You don't need to know any information about what triggered the action, just that the action was triggered.
Sender
You not only need to know that an action was triggered, but information about what object triggered the action. For instance, if you need to know which button triggered a certain action in order to change its properties.
Sender and event
You need to know the action was triggered, what object triggered this action, and the type of event that triggered the action. For example, if you need to know which button triggered a certain action in order to change its properties, and you will change them differently if they touch down on the button vs touch up vs double-tap vs etc. but you don't want to create a separate action method for each type of event.
Stonz2's answer covers it pretty well.
Some examples where you might want the sender:
Say you have a calculator app, and you have digit buttons and operator buttons. Rather than writing a different IBAction method for every button, you might write a -digitTapped action and an -operatorTapped action.
You could add tag values to each button, and then in your action method, interrogate the sender to see what it's tag is.
Another example would be a slider. You might use the sender parameter to get a pointer to the slider and fetch it's value.
(BTW, by default IB makes the type of the sender be id, which is an anonymous pointer. I usually change the type to be the type of the object that is triggering the action, like UIButton, UISlider, etc.)