I need to do action for next and previous button event for ios video player, here am using mpmovieplayerviewcontroller.
MPMoviePlayerController fires off MPMoviePlayerPlaybackStateDidChangeNotification when either the Prev or Next is tapped. There's no way to be notified whether each one is tapped.
The only way I found, was to create my own custom controls for backward and forward, adding a target to it to perform an action:
[prevBtn addTarget:self action:#selector(onClick:)
forControlEvents:UIControlEventTouchUpInside];
[nextBtn addTarget:self action:#selector(onClick:)
forControlEvents:UIControlEventTouchUpInside];
Then in your onClick method:
(void)onClick:(UIButton*)sender
{
if (sender == prevBtn)
{
// Do whatever when prevBtn is tapped
}
else if (sender == nextBtn)
{
// Do whatever when nextBtn is tapped
}
}
FYI: you must set the player's controlStyle property to MPMovieControlStyleNon to hide the default controls.
Related
I've three randomly generated images( actually UIButton) and when application runs it asks the user to choose one random image among the three(say a.png).after user select the image app will do some thing based on correct image was selected or not.
Now the question is that how can i identify that user select right image? I'm trying to get name of image that user select and then check it, but really no idea how to do that.
I've searched google for this but can't find something useful.
Can anyone help?
Thanks
When you really want to store the name of the image into the button, instead of using the tagproperty, you could set it to the title and hide the titleLabel (apple reference says: Although this property is read-only, its own properties are read/write. Use these properties to configure the appearance of the button label.):
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateNormal];
button.titleLabel.hidden = true;
[button setTitle:#"a.png" forState:UIControlStateNormal];
then you can access to the imagename with button.currentTitle.
While generating your UIButtons you can fill the tag property.
Then you can check if your sender.tag equals the value you filled in.
When you only want to check if the correct image is selected, you can set only this one to 1 and the other buttons to 0.
To know which button clicked u need to add tag say from 0, 1, 2
[yourButton setTag:0]; //set tag 1 and 2 also to other button
Now add same selector or method to all buttons:
[yourButton setTarget:#selector(buttonClicked:) forState:UIControlTouchUPInside];
// setTarget to other buttons too
method would be this:
-(void)buttonClicked:(id)sender
{
if([sender tag] == 0)
{
//button 1 clicked
}
else if([sender tag] == 1)
{
//button 2 clicked
}
else if([sender tag] == 2)
{
//button 3 clicked
}
}
Well if you are using UIButtons for this purpose, you could set Target Selector on each button and determine the UIButton using their tag values.
There's also one more method to help. But only of you use 'backgroundImage'.This action is should be linked with your buttons:
-(IBAction)photoIconClicked:(id)sender{
if ([sender isKindOfClass:[UIButton class]]){
UIButton *button = (UIButton*)sender;
_currentImage = button.currentBackgroundImage;
}
}
I am having some trouble with the UIButton.
This is my code:
I am not using the Interface Builder and I am a new programmer.
What I want is that when the button is selected the title changes and the button's transparency changes from half visible to fully visible.
Thanks in advance, -Marnix
The problem is that the if statement is just executed once, when you're creating the button. Inside MyCode2, add this line:
[Button1 addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchDown];
Every time that the button's pressed, the buttonAction method will be executed, so:
- (IBAction)buttonAction:(id)sender {
if(Button1.selected == NO) {
// Do your "NO" stuff
}else if(Button1.selected == YES) {
// Do your "YES" stuff
}
}
You have to declare this IBAction in your .h and add this method in your .m file.
First of all, you should set title for different control state like
[Button1 setTitle:#"UnTapped" forState:UIControlStateSelected];
Second, you don't need to put setTitle method inside of this check.
Besides, you need to put this check inside of a method of the button to make it work. If you didn't use IB, then just use addTarget: action: forControlEvent like
[Button1 addTarget:self actoin:#selector(changeButtonState:) forControlEvent:UIControlEventTouchUpInside];
Lastly, did you make the button keep selected after touch? If not, add
Button1.selected = !Button1.selected
in the method. All in all, your method should looks like this
- (IBAction)buttonAction:(id)sender {
Button1.selected = !Button1.selected
if(Button1.selected == NO) {
Button1.alpha = 0.5
}else if(Button1.selected == YES) {
Button1.alpha = 1.0
}
}
I'm showing an NSAlert with a custom view and three buttons. The custom view has two text fields and it allows the user to log in.
In the Mac App Store, an NSAlert with a similar design comes up. When the user clicks the sign in button, the NSAlert does not dismiss (until the credentials are verified). How does Apple keep the alert up?
Get the NSButton you want to behave differently. Change its target and action. (To call through to the original target/action, preserve them before you change them.)
NSAlert *alert = ...;
NSButton *button = [[alert buttons] objectAtIndex:...];
id oldTarget = [button target];
SEL oldAction = [button action];
[button setTarget:self];
[button setAction:#selector(verifyCredentials:)];
Alternatively, you may want to build the alert as a custom window controller and XIB (which is how Apple did it in the case of the App Store.) In that case, you have fine-grained control over button behaviour.
Swift 4 style
let alert = NSAlert()
alert.alertStyle = .informational
alert.messageText = "Y/N?"
alert.addButton(withTitle: "Y")
alert.addButton(withTitle: "N")
guard let window = view.window else { return } // for NSViewController
alert.beginSheetModal(for: window) { res in
if res == .alertFirstButtonReturn {
// "Y" action
} else {
// "N" action
}
}
I have a UISlider on screen, and I need to be able to detect when the user stops touching it. (so I can fade some elements away).
I have tried using:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
but this did not work when ending touches on a slider.
You can detect when a touch ends using two control events; try
[slider addTarget:self action:#selector(touchEnded:)
forControlEvents:UIControlEventTouchUpInside];
or
[slider addTarget:self action:#selector(touchEnded:)
forControlEvents:UIControlEventTouchUpOutside];
If you want to detect both types of the touchesEnd event, use
[slider addTarget:self action:#selector(touchEnded:)
forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
Instead of using touchesEnded: (which shouldn't be used for this purpose anyway), attach an action to the UISlider's UIControlEventValueChanged event and set the continuous property of the UISlider to NO, so the event will fire when the user finishes selecting a value.
mySlider.continuous = NO;
[mySlider addTarget:self
action:#selector(myMethodThatFadesObjects)
forControlEvents:UIControlEventValueChanged];
I couldn't get anything to capture both the start and end of the touches, but upon RTFD-ing, I came up with something that will do both.
#IBAction func sliderAction(_ sender: UISlider, forEvent event: UIEvent) {
if let touchEvent = event.allTouches?.first {
switch touchEvent.phase {
case .began:
print("touches began")
sliderTouchBegan()
case .ended:
print("touches ended")
sliderTouchEnded()
default:
delegate?.sliderValueUpdated(sender.value)
}
}
}
sliderTouchBegan() and sliderTouchEnded() are just methods I wrote that handle animations when the touch begins and when it ends. If it's not a begin or end, it's a default and the slider value updates.
I got a (stupid) question. I'm busy with a 'tap' app (i dont know how to name it) it's a game with a button in the middle of the screen and when a play start tapping the button it need's to count the click's.
Does anoyone know what i need to use (classes) in objective-c
Thanks
Hook up an action to the tap event :
[button addTarget:self action: #selector(button_tapped) forControlEvents: UIControlEventTouchUpInside];
And increment a counter when tapped (tapCount is a property of your view controller)
- (void)button_tapped
{
self.tapCount += 1;
}