What is the reset(_:) method - ios

I am learning iOS app development, and I came across a line which confused me:
Also add a call from reset(_:) so it works when you reset the app
But I can't find a reset(_:) function anywhere, both in ViewController and AppDelegate. Do I have to create the function, or is it something different?

So let's add the additional infos:
Your quote is from Intro to App Development with Swift, Apple's iBook., chapter 17.6 Polishing the Interface, in Disabling sliders subpart.
You missed the previous part:
#IBAction func reset(_ sender: AnyObject) {​} Open the Connections inspector. You’ll see that the button has been
connected to the Touch Up Inside event. This is the standard event
used ​for most buttons. Your reset button will set the value of each
slider to 1 and the isOn property of each switch to false. Add that
code to the new action method.
So your quote was talking about that method that you should have added previously on ViewController.swift in the chapter 17.5 Reset Button.

Related

can multiple buttons be attached to a single IBAction is Xcode 8?

I have tried to connect several buttons to a single IBAction in Xcode 8 however it does not work. I tried the practices outlined here Xcode8 cannot connect multiple/single uibuttons to single #IBAction in swift file still to no avail. can anyone shed some light as to why or if it is simply still just a bug?
Works fine for me in Xcode 8.1 on a new Swift 3 project. You have to control-drag from the button to the center side of the method to select it if it already exists rather than drag to the left side.
change your parameter from (_ sender: Any) to (_ sender: UIButton), and you're good to go.
There is no reason why several buttons should not connect to a single action function in your code. I had no trouble doing this when I tried it, as you can see:
However, if the nib editor interface won't let you form the connection, configure the action-target of the buttons in code.
Double check if you want to add action to a real button
Check if when you wrote the action, you used some customized class instead of UIButton, and the class of your button does not match.
I was able to resolve the issue by upgrading to Xcode 8.1 and including an _ before the sender argument. I believe this is just a bug from Xcode 8 because I tried the latter before upgrading and I was still unable to connect.

I'm getting a lock alert when trying to connect/add elements to the viewcontroller in Xcode 7.3

I have a ViewController that all of the sudden will not allow me to add a new element or connect an existing element to an IBAction. I've looked through sample code as well and googled the s*%& out of it without finding a solution.
My ViewController.swift file contains this code:
#IBAction func helpButtonTapped(sender: AnyObject) {
}
And I'm trying to connect a very basic button.
Also, note that I've double-checked to be sure that all elements and views are set to Lock: Nothing.
Any help would be amazing!
Go to menu Editor then Localization Locking then choose Nothing.
You probably have enabled one of the other locking options accidentally.

Selector not called on selecting menu item after force touch

I have this code, for responding to a menu item being tapped after a force touch:
class InterfaceController: WKInterfaceController {
override init() {
super.init()
self.addMenuItemWithItemIcon(WKMenuItemIcon.Pause, title: "Pause", action: Selector("test"))
}
func test() {
print("test")
}
}
When I force touch, the menu appears. When I tap the "Pause" button, test() is not called.
Any ideas why this might be?
Solved.
Though its the result of a silly mistake (aren't most problems?), I think this is something a lot of people will run into, so I'll keep it here with my answer.
I enabled force touch on the simulator, so I could show the menu. When I'm tapping again on the button, force touch is still enabled, so I'm force touching, thus dismissing the menu.
Solution: Disable force touch before tapping a menu button.
I wanted to comment with my thanks for this solution but I don't have the rep so I'll begin with, THANKS!
However I can also add an addendum to the solution by pointing out the shortcut keys are really handy here for switching between force press pressures.
⇧⌘1 - for shallow pressure (tap)
⇧⌘2 - for deep pressure (force press)
Make sure the iOS Simulator (watch) is focussed and you'll be good to go.
I'd like to add a small detail to make the SIM procedure more explicit because I actually saw this question and answer before and didn't perform it correctly because it wasn't totally obvious (to me at least).
Press ⇧⌘1 for all regular presses.
Press ⇧⌘2 before you use force touch.
Tricky part: After the menu item appears press ⇧⌘1 again before dismissing the menu item!

Xcode UI Test example

I have just recently learned about Unit Testing in Xcode. Now I am trying out Xcode 7 and I see there is a new group for UI Tests when I create a new project.
I watched the WWDC 2015 video and it was pretty good, but do you have a super simple example that I could go through myself? The video examples were a little too complex for me.
Notes
The answer below is my attempt to figure this out, but I welcome any better answers.
I have read these SO questions about UI Testing in Xcode but they are different: docs, reloading, App vs UI, ViewController, multiple, values and properties, pre XCode 7 projects.
Use Unit Tests to test the validity of methods in your classes. You use them to test the code you have written. (See my other example for setting up a simple Unit Test in Xcode.)
Use UI Tests to check the validity of the User Interface. Think of it like having your own robot to go through and do all the normal interactions with your app that a normal user would. This saves you the time of doing it yourself.
At the time of this writing, it is difficult to access many of the properties of the UI components, but just having a test go through tapping them and swiping them confirms that they are there.
Example
This is about the simplest setup and UI test that I could think of: a button that when pressed changes the text of a label.
Set up
Create a new project in Xcode 7+ for iOS 9.0+.
Make sure that Include UI Tests is checked
If you are adding UI tests to a project created before Xcode 7, see this answer. (File > New > Target > Test > Cocoa Touch UI Testing Bundle)
Add a UILabel and a UIButton to the storyboard
Create an #IBOutlet and #IBAction in the ViewController and make the label text change when the button is pressed.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var label: UILabel!
#IBAction func button(sender: AnyObject) {
label.text = "Hello"
}
}
Do the test
Open the YourProjectUITests file.
Put your curser in the testExample() method. (You can delete the comments)
Press the red Record button
In the app, (1) tap the label, (2) tap the button, and then (3) tap the label again. (4) Press the Record button again to stop recording. The following code should have been automatically generated for you:
func testExample() {
let app = XCUIApplication()
app.staticTexts["Label"].tap()
app.buttons["Button"].tap()
app.staticTexts["Hello"].tap()
}
Use the staticText lines as a starting point for making an XCTAssert. Now you should have:
func testExample() {
let app = XCUIApplication()
XCTAssert(app.staticTexts["Label"].exists)
app.buttons["Button"].tap()
XCTAssert(app.staticTexts["Hello"].exists)
}
Press the diamond on the left to run the UI Test. It should turn green when it passes.
That's it! This showed that the UIButton and UILabel exist and that the text of the label changed. If you want to see it fail (a good idea), you can change "Hello" to something else.
Further study
UI Testing in Xcode
Exploring the New UI Testing Features of Xcode 7
Xcode 7 UI testing, a first look
UI Testing in Xcode 7
#Suragch +1 for the answer. One thing I observed and want to share that every function inside the UI Test case must start with "test". You can append extra name after that. Only this way the button(for clicking to start the test) appears.

iOS 8.x Swift MPRemoteCommandCenter - Disabling audio playback controls

I'm currently experiencing either a bug or lack of understanding of Apple's Documentation.
Some background. I'm using AVPlayer to initiate a playback of audio. I'm using AVPlayer because it provides more precise controls over playback positioning (dropping into specific spots of a song).
When I implement the code below, the MPRemoteCommandCenter only displays one button (the play/pause button) and all other buttons (back and forward) are not present.
MPRemoteCommandCenter.sharedCommandCenter().playCommand.addTarget(self, action: "remoteCommandMute")
MPRemoteCommandCenter.sharedCommandCenter().pauseCommand.addTarget(self, action: "remoteCommandMute")
I can then toggle whether it's disabled or not just fine. According to Apple's documentation however you should be able to toggle the enabled property and it should hide the buttons entirely without having to add a target to them (at least from my understanding). Here is the reference to the documentation
If I do not include the .addTarget code to any of the buttons and simply follow the instructions from apple to disable said buttons, it does not work.
MPRemoteCommandCenter.sharedCommandCenter().previousTrackCommand.enabled = false
MPRemoteCommandCenter.sharedCommandCenter().nextTrackCommand.enabled = false
MPRemoteCommandCenter.sharedCommandCenter().playCommand.enabled = false
MPRemoteCommandCenter.sharedCommandCenter().pauseCommand.enabled = false
Based on this behavior I would expect that disabling all three buttons would in turn make them hidden however this is not the behavior at all in fact disabling them has no effect on them. You can still press them and they behave as if that code isn't present at all. However, if (as stated above) I add the .addTarget to the button it will become disabled if I set the .enabled flag to false.
Please correct my logic/understanding if this isn't the intended behavior but in my opinion this is absolutely backwards thinking on Apple's part. If my app doesn't support pausing forward or backward functionality the button shouldn't appear at all and disabling it shouldn't just turn it grey (like it currently does). It also shouldn't magically hide buttons if you don't add a target to them either.
On a side note, whenever I do add a target to the buttons they work just fine even if I don't want my app to have the forward and back capabilities.
So after experimenting with the different available button types and actions, it seems that the wording in the documentation could be better. In order to "disable and hide" a button one simply needs to add a target to the desired button. For example, likeCommand.addTarget disables the previous pause/play and forward buttons and hides them. However, if I set the playCommand.enabled to false, it will display said button as greyed out.
Edit: To address not actually sharing the code to which I'm referring...
import UIKit
import PlaygroundSupport
import MediaPlayer
class MyViewController : UIViewController {
private let remote: MPRemoteCommandCenter = MPRemoteCommandCenter.shared()
override func loadView() {
remote.playCommand.addTarget(self, action: #selector(playPause))
remote.playCommand.isEnabled = false
}
#objc private func playPause() {
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
This demonstrates the issue outlined such that toggling the isEnabled property does not do like the documentation states

Resources