Don't dismiss NSAlert when button is clicked - objective-c

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
}
}

Related

How do we control next and previous icon in mpmediaplayerviewcontroller

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.

Prevent hiding keyboard on presentViewController (showing popup)

There is a view with UITextView on it. UITextView is the only one possible firstResponder for keyboard. Let's call it "BackView".
There is another view which is PopUp (full screen view with transparent background which fades underlying view). Let's call it "PopUp".
PopUp works fine except when the keyboard is on screen. Presenting PopUp forces keyboard to be hidden and, when the PopUp is dismissed, the keyboard is shown again. Since the PopUp is not covering whole BackView it does not look good.
Is there any way to keep keyboard on BackView while PopUp is shown? (there is no need to use keyboard in popup).
PopUp contains:
full screen view with background set to black colour with alpha (to fade underlaying view)
several images, labels and Close button.
PopUp is shown with:
[self setModalPresentationStyle:UIModalPresentationCurrentContext];
[self presentViewController:vc animated:YES completion:nil];
Found solution:
Add PopUp view as a subview for current window
mb.server answered his own question (one that I had as well). But I just wanted to spell out the code explicitly for the benefit of others:
//instantiate popUpVC, then
popUpVC.view.frame = [UIScreen mainScreen].bounds; //assuming you wish the popover to occupy the entire screen; adjust as needed
UIWindow* currentWindow = [[[UIApplication sharedApplication] windows] lastObject];
[currentWindow addSubview:popUpVC.view];
Show:
if let popupVC = storyboard.instantiateViewControllerWithIdentifier("PopupVC") as? PopupVC
{
var frame = UIScreen.mainScreen().bounds
frame.origin.y = frame.size.height
overlayWindow = UIWindow(frame: frame)
popupVC.overlayWindow = overlayWindow
overlayWindow.windowLevel = UIWindowLevelAlert
overlayWindow.rootViewController = popupVC
overlayWindow.makeKeyAndVisible()
UIView.animateWithDuration(0.3, animations:
{
self.overlayWindow.frame.origin.y = 0
})
}
Hide in PopupVC
if let window = overlayWindow
{
UIView.animateWithDuration(0.3, animations: {
window.frame.origin.y = window.frame.height
}, completion: { (finished) -> Void in
self.overlayWindow = nil
})
}

iOS Display Different Image on Click

Using XCode, I am trying to figure out how to display a different image when someone clicks or presses down on one of my buttons before being taken to a second screen. For example, I have a contact icon on my home screen. When a user clicks the icon, it should change to a darker version on tap before going to the contact screen. Any help is appreciated.
-(IBAction) ButtonPressed :(id)sender
{
UIButton *tempButton = (UIButton *) sender;
int tag = tempButton.tag;
NSString *viewName;
switch (tag)
{
case 1:
[FlurryAnalytics logEvent:#"Contact-Screen"];
viewName = #"ContactScreen";
if( self.appDelegate.sound)
[Click play];
[self.appDelegate moveToView:viewName];
break;
}
}
Simple, you just have to set a different image for the buttons selected state:
[myButton setImage:[UIImage imageNamed:#"mySelectedImage"] forState:UIControlStateSelected];
Or in interface builder:
Additional UIControlState's include:
enum {
UIControlStateNormal = 0,
UIControlStateHighlighted = 1 << 0,
UIControlStateDisabled = 1 << 1,
UIControlStateSelected = 1 << 2,
UIControlStateApplication = 0x00FF0000,
UIControlStateReserved = 0xFF000000
};
The state of a control; a control can have more than one state at a
time. States are recognized differently depending on the control. For
example, a UIButton instance may be configured (using the
setImage:forState: method) to display one image when it is in its
normal state and a different image when it is highlighted.
As far as I've ever noticed in Xcode, whenever you decide to implement a customized button and don't specify a second image for use in the UIControlStateSelected state, Xcode automatically darkens the button for you when the user taps the buttons to show that they were, in fact, tapped.
The 0x7f answer is almost correct (the correct state value is 'Highlighted' and not 'Selected'). For Swift users, it would be
myButton.setBackgroundImage(UIImage(named: "myButtonImagePressed.png"), forState: .Highlighted)

iOS how to identify which image (button) is pressed?

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;
}
}

UIButton action not working

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
}
}