SenTesting for iOS programmatically press button? - iphone

So in some of the apple sample code I see things like this for application testing:
[viewController press:[calcView viewWithTag: 6]]; // 6
However when I try to do that with my own viewcontrollers/views all I am getting is "No visible interface declares press"... where is the documentation for application SenTesting on iOS, and in particular how do you go about doing UI testing (programmatically press a button, etc) within iOS?

Without seeing the sample code, I would guess that the view controller has a UIAction -press:. It should expect the nib to wire button presses to this action.
Instead of simulating the event, the test directly calls the action touch event would call.
After looking a bit, I think you may want how to programmatically fake a touch event to a UIButton?.
First you need to get a reference to the UIButton. If it's already set as an outlet in the view controller, you can use that. So, I'll assume your viewController has an IBOutlet for that UIButton named 'button'
[viewController.button sendActionsForControlEvents:UIControlEventTouchUpInside];
For the Swift 2.0 solution
viewController.button.sendActionsForControlEvents(.TouchUpInside)

Related

Shake event detection in all view controllers

Is this possible in iOS, or do I really have to register a shake event detection in every single view controller?
I want that a user of my app can shake his iPhone to return to the root. Regardless of the current view controller.
I've done this by writing a category on UIViewController. This way you don't need to subclass anything, you can implement it even if the user interface has been finished already.
In my implementation, the VCs will respond to the shake unless the specific VC has opted out of the mechanism. Opting out is done by setting a BOOL ivar to NO in the specific implementation. The shake mechanism will look for this ivar using key-value-coding and ignore the shake if the ivar has been set.
I'd further refine this by allowing only the VC to respond which is currently visible.
edited post (old information was wrong)
in your case I would write my own ViewController which subclasses UIViewController, implements the motion-delegates of UIResponder and will then call the popToRootViewController on the navigationController-property of the ViewController. And everytimes you create a new ViewController you should subclass your ViewCOntroller and not UIViewController. So every ViewController is able to receive the shake-event but it is only written once in your code :)
This is just a guess, but maybe you could do it by subclassing UIApplication (not your app delegate, the actual application). UIApplication is a UIResponder, so you can make it the first responder, and provide a motionBegan or motionEnded method on it.

Controls on UIView does not work after adding uiview to uiwindow

I am trying to work on a universal app for iOS platform, I created a window based application with core data support.
In my AppDelegate_iPhone I added this line in applicationDidFinishLaunching
[window addSubview:myiPhoneMainView.view]; of course I have created & synthesized the myiPhoneMainView variable.
In my MainWindow_iPhone.xib I added a view controller set its class to MyiPhoneMainView and controlled dragged from app delegate to MyiPhoneMainView.
Then I went on & added controls on my MyiPhoneMainView. Everything works fine MyiPhoneMainView gets loaded but the problem is none of the controls are responding to the user events.
I have checked user interaction is enabled on all of them. Niether uibotton nor uitextfield nothing seems to respond to anything.
Can somebody please point out what am I missing.
Thank you .
It sounds like your connections from your view's controls are not linked to the IBActions in your view controller.
Are you loading the controller via initWithNibNamed: or are you just using init? If using initWithNib, then have you linked up the controls in the view with the IBOutlets?
If yes to those, then make sure you are not creating a new view (in the init method or the viewDidLoad of the controller) that replaces the one created in the xib.

creating a reference for use in identifiying if a button was clicked

How is it possible create a reference for an uibutton to check if it was pressed and do something like make the value for that button true in order to be able to use that value later on. I heard of ivars but i havent seen any documentation on how to use them and im not sure if they would work for my ibaction uibutton...
This is really a fundamental question and a full explanation will take along time.
Basically, you need to understand the concept of MVC (Model-View-Controller). In this case, the "View" will be your UIButton created in Interface Builder. It needs to have a target/action setup to point to some "Controller". This will be a custom class you design that performs some action when the UIButton is released. This controller can also track finger up/down/move/drag and what not. This Controller will store the fact the button was pressed in a "Model" class.
Here is some more reading:
http://developer.apple.com/iphone/library/documentation/general/conceptual/devpedia-cocoacore/MVC.html
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaDesignPatterns/CocoaDesignPatterns.html
In the simplest cases, this can be done with Core Data and IB without writing any code and simply making connections.

UIWebView/MPMoviePlayerController and the "Done" button

I am using the UIWebView to load both streaming audio and video. I have properly set up the UIWebView delegate and I am receiving webViewDidStartLoading and webViewFinishedLoading events perfectly. The webview launches a full screen window (likely a MPMoviePlayerController)
Apple's MoviePlayer example gets the array of Windows to determine which window the moviePlayerWindow is for adding custom drawing/getting at the GUI components. I believe this to be a bad practice/hack.
My expectation is that I should be able to figure out when that button was clicked by either a delegate method or an NSNotification. It may also be the case that I have to poke around subviews or controllers with isKindOf calls, but I don't think those are correct approaches.
Are my expectations incorrect, and if so, why?
What is the correct way to bind an action to that "Done" button?
There isn't an MPMoviePlayer instance method that covers this. You can use - (void) moviePlayBackDidFinish:(NSNotification*)notification to find out when the movie has finished. Or you could overlay the existing Done button with your own and have complete control that way.
You can also use MPMoviePlayerWillExitFullscreenNotification in order to conrol the action provided that youe MoviePlayer is in fulscreen mode.

Utility Application infoButton Stays on Top

I used a utility application template to create my application, but I am using a UIImagePicker to pick some photos. The little infoButton, the i with a circle around it, shows up when the picker is displayed. If the infoButton was not in another class then I could call infoButton.hidden = YES; and it would hide it. Any ideas how to solve this problem?
Thanks,
-Pat
I took a look at the utility application template, the view controller with a gray background and an info button is MainViewController, and the instance is also the "mainViewController" property of the application delegate.
It seems that you have already created an IBOutlet to the infoButton, so, you can access the button by calling [UIApplication sharedApplication].delegate.mainViewController.infoButton .