NSPushOnPushOffButton Not Staying "Pushed In" - objective-c

I'm trying to add a button to a view programmatically and I'd like it to stay "pushed in" when clicked, until it is clicked again. From what I've read it seems like the button to use for this would be NSPushOnPushOffButton. Here's Apple's discription for it:
The first click both highlights and causes the button to be “pushed
in” if the button is bordered; a second click returns it to its normal
state. This option is called “Push On Push Off” in Interface Builder’s
Button Inspector.
It wasn't working in my program so I made a SSCCE, which I'll include below. My problem is the button isn't staying in when clicked, but rather popping back out like a normal button. I'm not sure if I'm missing something about how this button is supposed to perform or if I'm simply implementing it incorrectly (This is the first time I've tried to do a UI programmatically), but other button types such as NSToggleButton work so unless there is something special about NSPushOnPushOffButton I don't see where I went wrong. if someone could suggest a better solution or explain where I went wrong that would be great. Thanks!
#import "AppDelegate.h"
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSRect buttonFrame = NSMakeRect(0,0,200,200);
NSButton *button = [[NSButton alloc]initWithFrame:buttonFrame];
[self.window.contentView addSubview:button];
[button setButtonType:NSPushOnPushOffButton];
}
#end

It seems that you have to set a "bezel style", for example
[button setBezelStyle:NSRegularSquareBezelStyle];
After adding that to your sample code, it worked as expected.

Related

Can't disable button

Ok, I've been looking through forums the past couple of days and trying every way that I can to disable this button, but nothing is working. I'm assuming that it's just some syntax I need due to the way that I'm calling the view controller. I can change label texts, but not alter buttons in any way. Here's what I have tried to change the button, but nothing is working:
in my .h file, linked with the button with control-drag:
#property (weak, nonatomic) IBOutlet UIButton *btnDelete;
and here is what I have put in my .m file in the viewDidLoad and also tried in viewDidAppear:
_btnDelete.enabled=false;
_btnDelete.hidden=true;
_btnDelete.userInteractionEnabled=false;
[_btnDelete setEnabled:NO];
[btnDelete setAlpha:0.0];
[btnDelete setNeedsDisplay];
None of these appear to do anything to the button.
Here is how I am calling the view controller that has the button on it.
Detail *Det = [self.storyboard instantiateViewControllerWithIdentifier:#"Detail"];
[Det setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentViewController:Det animated:YES completion:nil];
I also tried to add the following lines right after the above, just to see if that would work, but it did not.
Det.btnDelete.enabled=false;
Det.btnDelete.hidden=true;
Like I said, I've went through forums and tried to find diff ways to modify the button, but nothing is working. The button is not attributed, just plain text. I cannot modify the button in any way that I can see, but I can modify other controls on the page, like labels, just fine.
Thanks in advance for any help on this.

NSButtons Inside NSPopover View Controller

I've developed for Mac before but this is the first time I've attempted to use the NSPopover control, which seemed like a great idea to start out with but so far is causing me no end of problems. The applciation is a menu bar application. I have two NSButton objects in the NSPopover's view controller, the NSPopover is being created programmatically in another subclass of NSButton, the same button which it is being shown relative to. This NSButton that it is being shown relative to is contained along with some other buttons in an NSMenuItem
The popup, containing the two buttons, is being shown fine (see screenshot below), however, despite the 'Yes' button being highlighted with a focus ring, neither button responds to click events, they do not even graphically click in like I would expect them to.
And this is the code that creates the NSPopover and positions it onscreen:
someViewController *confirmationDialogue = [[someViewController alloc] initWithNibName:#"someViewController" bundle:nil];
popOver = [[NSPopover alloc] init];
[popOver setBehavior:NSPopoverAppearanceMinimal];
[popOver setBehavior:NSPopoverBehaviorTransient];
[popOver setContentViewController:confirmationDialogue];
[popOver showRelativeToRect:NSMakeRect(0, 0, self.frame.size.width, self.frame.size.height))
ofView:self
preferredEdge:NSMaxYEdge];
Has anyone got any kind of solution/workaround to this?
Thanks in advance :)
P.s. This is my first question on SO, so I hope I've provided enough information but I'll give any more details as needed.

How to prevent a UIButton from changing when touched?

I have a button that pretty much takes an entire xib so that I can push another viewController on the screen if any part of the original view is touched. I've created an IBOutlet to this button and it works great, however I don't want the entire window to turn "blue" when it is touched (similar to when a small button is pressed). I've tried the following two methods that I found in a similar SO post: How can I prevent a UIButton from highlighting when pressed?, but neither method works. Is there something else I need to set up?
- (void)viewDidLoad
{
[super viewDidLoad];
self.entireWindowButton.adjustsImageWhenHighlighted = NO;
[self.entireWindowButton setImage: [self.entireWindowButton imageForState:UIControlStateNormal] forState:UIControlStateHighlighted];
}
I realise this is old question but if you set the UIButtons type to custom then "adjustsImageWhenHighlighted = NO" will work and it is set to NO by default.

UIPageControl value changed not firing

I have an issue with the UIPageControl. I simplified the issue for clarity below:
I put a button and a UIPageControl in my app with Interface builder and put the following in the code:
- (IBAction)tappedButton:(id)sender{
self.pageControl.currentPage = 3;
NSLog(#"Tapped Button");
}
- (IBAction)changePage:(id)sender{
NSLog(#"PAGE Changed!!!!");
}
I attached the Value Changed action to the pageController via Interface Builder.
When I tap on the button, I see the output "Tapped Button" and the third dot is highlighted... but the changePage method is never called.
Any ideas?
The changePage: method will only be called when the users request a change of page using the UIPageControl. It won't be called when the user taps the button. If you want to call the changePage: method when the button is pressed called it explicitly:
[self changePage: nil];
UIPageControl doesn't actually change pages for you. You have to write that code, inresponse to the valueChanged event. So, you want to connect that event of your UIPageControl object to your output (tappedButton: I guess), and then call your changePage: method to actually change the UI. Does that help?
There are some example projects referenced from the Apple docs that use UIScrollView and UIPageControl together. Check 'em out.

How do I add a navigation bar's Done button to a popover's passthroughViews?

I'm working in an iPad app that has a split view with a navigation controller in the detail view. The deepest view that can be in the navigation stack is an edit view where the user can edit data. I put an edit button as the rightBarButtonItem and when editing starts, change it to a done button.
When editing commences and the user touches on a particular field, I present a popoverview with a list of possible choices filtered by what they are typing - a form of autofill based on all the values of that field in all other objects.
This works fine, except if you try touching on the done button. The popover eats this touch and dismisses itself. So the user has to touch done again.
I tried using the uipopovercontroller's passthroughViews property, but UIBarButtonItem is not a view and there is no documented way to get the view for the done button or even the navigation bar. I can access the variable in gdb, but it isn't accessible via KVC.
Any ideas on how I can prevent the need to tap done twice?
I've thought about a gesture recognizer on the window, but that seems messy and I'd have to handle rotation.
In case anyone gets here from google, copypaste from other question:
The only solution I found for now is to create UIBarButtonItem with custom UIButton using
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//code for styling button
UIBarButtonItem *b = [[[UIBarButtonItem alloc]
initWithCustomView:button]
autorelease]
and then
popoverController.passthroughViews = [NSArray arrayWithObject:b.customView];
But be prepared - you cannot create UIButton that looks like UIBarButtoItem. I ended up with creating image that reassembled UIBarButtonItem.