Detect when pullToRefresh is called on swift - swift

Is there a way to detect when the pullToRefresh has been called on a Parse - PFQueryTableViewController?
I've found some of that on objective-c, but nothing on swift.

As far as I know, pullToRefresh is a private implementation, so you can't override it to detect calls. However, pulling to refresh will trigger the loadObjects function, which in turn triggers the objectsWillLoad and objectsDidLoad methods. You can override the 2 methods to do whatever you want to do.
The only drawback is that objectsWillLoad and the did load counterpart will be called even when the view is initially displayed.

Related

pickerDidSettle() not called

In one of my Watch Extension's interface controllers I have several WKInterfacePicker elements, and I need to know when the user has selected a value. According to documentation, WKInterfaceController should be able to implement pickerDidSettle(_:) method that has the corresponding picker element as parameter. For some reason the method never gets called when I use the pickers. Here is the basic structure of my implementation:
override func pickerDidSettle(picker: WKInterfacePicker) {
// Code inside this block is not called
}
If I mark the function with an #IBAction attribute and connect them with the picker elements in interface builder, the instance method works. However, this apparently prevents me to assign picker actions that receive all the picker values through which the user is scrolling.
#IBAction
override func pickerDidSettle(picker: WKInterfacePicker) {
// This function gets called, but blocks other actions
}
My interface controller inherits from WKInterfaceController and conforms to two custom protocols. How should I implement the method?
Edit: The issue was related to a possible bug in WatchKit, where pickerDidSettle(_:) will not be called without an existing #IBAction connection to the controller. I assume it is a bug, because related instance methods pickerDidFocus(_:) and pickerDidResignFocus(_:) work independent of the connection.
Sometimes this issue occurs, when something gets 'out of sync' between Xcode and the Simulator.
Just close the Simulator and clean and rebuild your app (via 'Product/Clean Build Folder') to recreate the 'sync.

How do I reference a controller from within the object it created?

So I have a PanelController which is a NSWindowController, and it instantiates an object called FileUploader. But inside of FileUploader, I want to be able to update some text on the Panel informing the user of the progress that has been made so far. But I don't know how to reference PanelController given that I can't instantiate a new one.
Am I supposed to use ApplicationDelegate as the go-between, and if so how do I reference that? If so, do I need ApplicationDelegate to instantiate both PanelController and FileUploader, but then I still don't understand how to reference the ApplicationDelegate.
Also consider using blocks which is another more modern apple approach to callbacks. Apple is using it more and more in their new APIs.
Here's a specific SO on updating UI and also making it async:
GCD, Threads, Program Flow and UI Updating
Here's another SO article on blocks & delegates as callbacks
How to simplify callback logic with a Block?
Apple recommends using Delegates in such scenarios.
Here are some SO answers on how to make delegates:
How Do I Create Delegates in Obj-C
How Does Delegate Work in Obj-C

iPhone: I've come up with an interesting way I could get around an API limitation. Will this work?

The problem that I'm trying to solve is this:
When using a UIImagePicker, it adjusts itself as the device is rotated. I don't want it to do this, rather it should constantly stay in the portrait orientation. I've googled aplenty and haven't found any solution (just others with the same unsolved problem). The only answer was to use a private API call, I would rather not risk doing this.
-
However, I came across 'method swizzling' - a way of swapping method names at runtime. What I'm planning to do:
Add a category to UIViewController that defines a methods such as 'customShouldRotate' defining custom behavior that won't allow rotation.
Swizzle these methods with the default rotation methods of UIViewController at runtime.
-
After a bit of thought though, it might not be that simple. Here's why - any answers to the following would be much appreciated.
The method I'm swizzling is in UIViewController, not UIImagePickerController. I can't swizzle in the image picker directly, I've already tried via subclassing and it appears to be a subview's controller that's handling the rotation rather than UIImagePickerController itself. Will swizzled methods pass down to subclasses, if not then is there a workaround to do this?
It may not be a normal rotation method from UIViewController that is being used in UIImagePickerController. Is there any way to tell?
Will Apple be OK with method swizzling? I'm not using a private API call, but then again I wouldn't be overly surprised if they rejected it anyway.
-
Is this whole idea feasible/worth trying - or, if someone's actually solved the original problem in a different manner then that would be even better.
Method swizzling is fine: Apple check for private APIs using a static analyzer. If you can overload, swizzle, or do what you will to achieve the same effect without calling a private API it won't be picked up by the analyzer.
The only reason Apple may reject you for is for modifying 'standard' behaviour - but I suspect this won't happen. The worst thing is your app gets kicked back to you and you have to reinstate the original behaviour.

Can someone please explain delegates in objective-c?

I have been using Objective-C for a while and pretty much understand most of its features. However, the concept of delegates eludes me. Can someone please give a succinct and easy to comprehend explanation of what delegates are, how they are used in the iPhone SDK, and how I can best make use of them in my own code?
Thank you!
There are a couple main reasons to use delegates in Objective-C, which are subtly different:
Enhancing the base functionality of a framework class. For example, a UITableView is pretty boring on its own, so you can give it a delegate to handle the interesting bits (creating table cells, adding text to section headers, what have you). This way, UITableView never changes, but different table views can look and act very differently.
Communicating to parent objects in your dependency hierarchy. For example, you may have a view with a button that the user may push to do something that affects other views. The view will have to send a message to its parent view, or perhaps the view controller, so that it can create or destroy or modify other views. To do this you'd pass the parent object into your view, most likely through a protocol, as a weak reference (in Objective-C, an assign property). The view could then send any message declared in the protocol to the parent, or delegate, object.
This approach need not involve views. For example NSURLConnection passes event back to its delegate, which may be the object that created it, using this mechanism.
Essentially, all a delegate is, is an object that accepts feedback from another object. Put simply, when stuff happens to an object, it tells its delegate (assuming it has one).
For instance, lets say I have a UIViewController with a UITextView placed in the middle of the view. I set up my UIViewController to be the delegate of the UITextView. Then, when certain actions are performed on the text view (begin editing, text changes, end editing, etc), it tells it's delegate so it can do whatever logic it needs to do, like spell checking every time characters change, or dismissing the keyboard when it receives a return key press.
Delegate methods perform a similar function to callback functions in C.
Hope that makes sense :)
Best and simple concept I got from a Lynda.com Tutorial was: When you set a Delegate it means you have been given work to do. So, if you want to use methods that are written in a protocol method, you must implement them by searching in the Delegate Class Reference and using them. I hope it helped.
By the way, Delegates are excellents. They are your friends. They have been made to make your life as a programmer much easier.

Delegation, some example of code? How object delegate to other

I would like to gain a better understanding about the delegation. Can somebody please paste a good code sample of delegation and explain how it works?
There is a pretty good example at: http://en.wikipedia.org/wiki/Delegation_pattern#Objective-C_example
In this example, MyCoolAppController creates and object of type TCScrollView, and sets the "delegate" property of the TCScrollView to self. This means that when the TCScrollView calls
[delegate scrollView:self shouldScrollToPoint:to]
it is asking the MyCoolAppController (the delegate of the TCScrollView) to perform some calculations and see if it is ok to scroll. You can say "MyCoolAppController is the delegate of TCScrollView" to describe this; TCScrollView asks MyCoolAppController to do some work on its behalf.
Do you mean .NET or Java or some other language delegate?
A delegate in .NET parlance is nothing more than a function pointer, or in other words a variable that points to a block of executable code. They can be used in may ways. One way is to use them in the context of events. Lets say you have an ASP.NET page and you are using the MVP (Model View Presenter pattern on that page). You want your presenter to be notified of the click event of the save button on the view. You can define an event on the views interface, but in order to subscribe to that event and to take action on it you need to register a method that gets fired when the event is raised. For example:
public class ClassThatRegistersForEvent
{
public void InitializeView(IView view)
{
view.SaveButtonClickedEvent += delegate{
// do stuff in here when the event is raised
}
}
}
public interface IView
{
event System.EventHandler SaveButtonClickedEvent;
}
Here's an answer I wrote explaining delegation: https://stackoverflow.com/questions/1089737#1090170
A delegate is a way to respond to events. In other languages you would probably do this by subclassing. For example, say you have a table view. You could subclass the tableview and override the tableView:didSelectRowAtIndexPath: method, but that would get messy and create an unnecessary subclass (along with the fact that its not reusable) Instead, you create a TableViewDelegate class and tell your table view about it (tableView.delegate). This way, the method will automatically get called when something happens. This is a really clean solution to event-handling.
After you write a few apps that involve delegates (table views are the big ones), you'll get the hang of it.