Reputation: 145
Everyone on this site has been very helpful on my schooling of iOS programming, but I've run into a brick wall with a very simple feature. I've found Apples documentation on implementing the factory keyboard click sound upon a button touch, but what Im not getting is the "creating a sub lass of UIView" part. I have a very basic calculator that I've built, the buttons are made of standard Round Rect's that I want to make the simple click sound. So Apple says:Adopting the UIInputViewAudioFeedback Protocol Perform the following three steps to adopt the UIInputViewAudioFeedback protocol:
In your Xcode project, create a subclass of the UIView class. In the header file, indicate that the subclass conforms to the UIInputViewAudioFeedback protocol, as follows: @interface KeyboardAccessoryView : UIView { }
Now I am using the standard UIViewController tied to a xib with all my buttons. Am i to: Create New File, name it as a subclass of UIView, JUST so i can implement this sound? That doesn't make sense to me, and if this is really amateur stuff I apologize, but I'm learning from many different places. Thanks in advance.
Upvotes: 1
Views: 953
Reputation: 160
You have to set your custom input view as the inputView
property of the object that is supposed to be the first responder (For example, a UITextField
instance), and the inputView
(your custom input view) must conform to the UIInputViewAudioFeedback
protocol. And to actually play a click sound: [[UIDevice currentDevice] playinputClick]
.
For example:
@interface MyCalculatorDisplay : UIView
// ...
@end
@interface MyCustomKeyboard : UIView <UIInputViewAudioFeedback>
// ...
@end
// Then, somewhere in your controller:
MyCustomKeyboard *keyboard = [MyCustomKeyboard new];
MyCalculatorDisplay *display = [MyCalculatorDisplay new];
display.inputView = keyboard;
Upvotes: 1