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.
Related
I'm using Mvvm-Light to create a binding to a UIButton with the SetCommand extension. I can just call it in ViewDidLoad(...) but I want to connect it in ViewWillAppear(...) and disconnect it in ViewWillDisappear(...) like I do with the rest of my bindings. So all bindings are only active when the view is visible. If I do it this way currently then SetCommand is called every time I navigate back to the view and the RelayCommand is fired multiple times, once for every call to to SetCommand.
Is this possible? And if not, then why not?
Why do you want to disconnect a command?
If a UIViewController has disappeared, all of his controls can't be touched and seen. So the command will only fire when it appears again, I think this has already fitted your request.
If you do want to remove this command in the event ViewWillDisappear() you can use:
button.RemoveTarget(null, null, UIControlEvent.AllEvents);
This will remove all the events the button has. As you say when you SetCommand() again in the event ViewWillAppear() the RelayCommand will only be called once.
Goal:
I need to be able to tell when a user is interacting with an iPhone app.
After 5 minutes, I will auto logout the user, but I don't want to do that if the user is interacting with the app.
I have read about shouldReceiveTouch:, but that it will be called if a user touches something. I could put login in there to reset the 5 minute timer.
How do I make it so that if any view on any screen is touched, the 5 minute timer starts over?
Is it possible to do this without implementing shouldReceiveTouch: everywhere?
Any help is appreciated.
There are at least 3 approaches:
(1) The best one I think is suggested in this post. Basically you would override -sendEvent: on UIApplication. But someone mentions this might not work anymore since iOS 7 - YMMV.
(2) Another, less clean solution would be to use method swizzling. You could replace a method on UIView for your own implementation, e.g. -touchesEnded:withEvent: or -hitTest:withEvent:.
(3) If your app doesn't use any 3rd party view controller, you could create a custom base class of UIViewController and make all your view controllers (except login view controller and anything shown before) inherit from this base class. This view controller would have a gesture recognizer or perhaps override a method like -touchesEnded:withEvent: or -hitTest:withEvent:.
For all approaches you would probably use a singleton to keep track of the last touched moment. Perhaps the singleton would use a countdown timer. The singleton could have a multicast delegate or use NSNotificationCenter to inform other view controllers that the timer has run out.
P.S.: I should note that you should consider method swizzling carefully. If some other project or library swizzles the same method, you will never know for sure which implementation is used for the swizzled method.
I have a view controller with 3 sliders. I only have 1 function that needs to be called every time a slider value changes - a value change in any one of the 3 sliders should cause call to that function.
How do i do that?
If I try to use outlets then I am only able to link only one UISlider.
You can simply connect the Value changed event of all sliders to the same IBAction:
If you want to connect them as outlets (which is something entirely different) you will have to use an IBOutletCollection:
If I try to use outlets then I am only able to link one UISlider.
You need to connect the slider to a target and action, not an outlet. The target is the object that will receive a message when the slider changes, and the action is the message that will be sent to the target. An outlet, on the other hand, is a reference to some object; for example, if you connect a slider to an outlet in your view controller, the view controller will then have a reference to the slider that it can use to send messages to the slider.
I have a simple app in Swift with just a few views:
A UIWebView
some TableViews
and another view with some data I download from my server
It all works well until when using the app I press the home button, leave there for a while then the iPad goes on sleep mode. A few days later I tap on the app icon and it won't start:
first tap on the icon will select the icon (goes a little darker) and deselect it a few seconds later
second tap will launch the LaunchScreen and crash a few seconds later
double tap the home button and quit the app will sometimes work
I'm just wondering if there is something I need to set on my code to handle idle/long periods of inactivity in something like viewWillDisappear or other methods?
If so I already have this in all my controllers:
override func viewWillDisappear(animated: Bool) {
timer.invalidate()
webView.removeFromSuperview()
}
Maybe I need to call super. in there too? or something else I'm missing?
You should definitely call super in your viewWillDisappear(animated:) method. See UIViewController Class Reference documentation. Also you might want to confirm why you are removing your webView from the view controller's hierarchy.
Discussion
This method is called in response to a view being removed
from a view hierarchy. This method is called before the view is
actually removed and before any animations are configured.
Subclasses can override this method and use it to commit editing
changes, resign the first responder status of the view, or perform
other relevant tasks. For example, you might use this method to revert
changes to the orientation or style of the status bar that were made
in the viewDidDisappear: method when the view was first presented. If
you override this method, you must call super at some point in your
implementation.
You probably have some null pointer exception and crash. Maybe you are calling some variable that is not set (and checked if not null).
Try disabling app funcionality (like downloading, storing and using data from server) and see where you app starts working normal again and then procede from there.
Sorry for vague answer but withouth code and maybe some log it is really hard to give specific answer.
And NO, you dont have to do anything special to handle idle/long periods of inactivity.
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.