iOS Objective-C IBAction runs the code in method twice - ios

I tried to run this method of code
- (IBAction)signInButton:(id)sender {
NSLog(#"Run Action %#", #"Here");
}
The result of this code log the "Run Action Here" twice in the console.
I initially loaded all my project import file (.m and .h) in one header file "Loader.h", I taught this was the cause, but I still experience the same issue even after I dissembled the header file.
Same Issue happens on other view controller.
What am I doing wrong ?
Thanks in advance.

It sounds like you've connected the action to your button or other UI element for two different events. For example, if you connect it to both the touch down and touch up events, a single tap of the button will trigger the action twice.
One thing you can do to diagnose the problem is to control-click on the view controller containing the action in your nib or storyboard and look at the Received Actions section near the bottom of the resulting popup. You'll likely see your action connected twice.
Another option is to set a breakpoint in the action and take a look at the sender parameter each time you hit the breakpoint. This will show you what object is triggering the action each time.

This seems to be some problem with the logging. It indeed logs twice from IBAction handlers. I put NSAlert, to make sure it's called twice, but it was called once, nevertheless the log was printed twice in the console.

Related

Can you disconnect a command after it has been set with Mvvm-Light on Xamarin.iOS

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.

Swift IOS app won't start after idle for over 1 day

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.

reloadRootControllersWithNames:contexts: does not load new InterfaceControllers

I have my WatchKit app (WatchOS1) set up in the following way (names have been changed to be project unspecific):
InitialInterfaceController - The main entry point of the watch app. This controller is only used to load several instances (using the same identifier repeatedly in the NSArray) of the next view using reloadRootControllersWithNames:contexts: (called from awakeWithContext:).
FirstInterfaceController - This Interface controller should be what is first displayed for the pages.
However this does not work - I get left with the blank InitialInterfaceController screen. If however I call [self presentControllerWithNames:contexts:] it works as expected, but includes the cancel button, which is not what I want.
I have seen people suggesting to use this method to dynamically create multiple page navigation scenes, but I cannot see why this doesn't work. The FirstInterfaceController's awakeWithContext: is never called.
Has anybody had this problem or is there a fix available?
Do you have the right value for the Identity in the interface controller's attribute inspector? (I have seen issues when the identity and class name are the same, make sure they are different)
Is that interface controller added to the right module in the identity inspector?

Knowing the exact method call when tap on iOS app

Is there any quick way to know which method is called when we do any activity on iOS app. I have the code but this is written by some other team and I am trying to find out the action that is happening when I tap on iPad. Is there any way to know about it. I tried putting break points but it does not help.
Following ways out of many can help you:
Find out all methods that have IBAction mention in the start in your .m file. Put a break point on them.
Goto .storyboard or .XIB file of your respective controller, in the right pane check Connection inspector. This inspector will be divided into two halves first belongs to IBOutlet, you have to check the second one which is IBAction. Find them in your .m file and put a break point on it.
Check .h file for any IBAction methods, find its implementation in .m and put a break point on it.
Trace any IBAction selector(s) specified via addTarget function.
Hope it can help!!

UITableViewCell getting _accessibilityUpdateRemoveControl on deallocated instance

Edit: While my comments have an iOS 5 working example, I am still getting this for other versions. I've now implememted a test to only register and dequeue cells if iOS 5, but it's really puzzling!
still receiving _accessibilityUpdateRemoveControl exceptions, strange nuisance, appears to be something with the edit controls, nothing is retained so nothing needs deallocing, but will try, and post the answer if I find it!
This was working yesterday, and now it's not... I changed nothing!
Edit: Turns out, while reloadData causes the crash, the crash does not occur without my custom tableViewCell... hmmm, something about removing the + sign, but it doesn't happen with deletion!
Actual error is this:
[CustomTableViewCell _accessibilityUpdateRemoveControl]: message sent to deallocated instance.
What's funny is, the remove button works. Essentially it removes the item from an array, adds it to another, basically putting it "to another table". No crashing, works fine.
If I remove the line that reloads the data in the table, after the insert button adds it, it also works. Eg: Don't immediately reload the data, close window, come back, everything displays fine. The exact line, so far, that crashes it is in
[theTable reloadData], but that line, for the other table (as I update both) doesn't crash at all. Actually, thanks to that, I'm gonna view the headers for UITableView's functions, and view other answers with that specific line. I just didn't see this, anywhere, after searching for that weird function call.
I'm ensuring my cell is within memory, and even quit dequeuing just to ensure it's working. I'm stumped with this, hopefully will have solution in an hr or less.
Thanks
I stepped through Apple's code, line by line, read the name of every function and noticed this:
editControlWasClicked: (id) clicked
is called just before crashing. I combined that with the error message, and the fact I call [table2 reloadData] before this is called, and pieced those pieces together.
The cell is erased (so it moves to the other table), but somehow calls its system callBack "editControlWasClicked" after the table reloads... since it's on the main thread, I'm guessing the table stuff is multi-threaded... how else would it call these in order but do that After the reload??
So to test this, I used the "afterDelay" function, and low and behold, it worked.
You may be asking why I'm using an add edit control in one and subtract in the other... there is a purpose to that.
So, possible solutions: 1) use the afterDelay method of selectors.
2) Write a custom IBAction ('cause it's a xib) or otherwise use custom images and functions to ensure that doesn't get called back.
Note, 2 involves writing an extra delegate so that messages from the cell can reach the view controller.
Basic solution: use iOS 5, use the queuing, otherwise do one of the above solutions or figure out the threading/hooks and find a way to do this without delaying. (I would prefer such if I can find it)

Resources