How to make UIActionsheet recognize which button was most recently pushed? - objective-c

I'm currently implementing a UIActionsheet that has two buttons.
Each of the buttons save different values into [NSUserDefaults standardUserDefaults]
The implementation i have atm:
-(void)foo{
UIActionSheet *mySheet = [[UIActionSheet alloc] initWithBlabla:blabla];
[mySheet showFromTabBar:myTabBar];
[mySheet release];
{
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == x){
[[NSUserDefaults standardUserDefaults] setValue:foo forKey:bar];
{
else{
// bla
{
}
Now, for the issue i'm having at hand.
I would really like the actionsheet to recognize which of the buttons that was clicked most recently and present that in some way.
So for example, the first time the actionsheet shows, both buttons are gray as default, one button gets pressed and the actionsheet goes away.
The second time the actionsheet shows, it shows up just as the first time. And i'ld like the actionsheet to give the user feedback regarding which of the buttons was most recently pushed. (a different coloring or anything really).
If i could acces the buttons generated within the actionsheet this would be a no-problemo.
In pseudo-code i'd probably want kind of solution as below
UIActionSheet *myActionSheet = [[UIActionSheet] alloc] initwithBlabla:blabla];
if([[userDefaults objectForKey:#"foo"]isEqualToString:#"bar"])
{
[myActionSheet setHighLighted:otherButton2];
}
I would like the actionsheet to recognize what the userdefaults for the key foo is and make adjustments to the buttons within the UIActionSheet.
Any tips and/or pointers on how to implement such a thingie will be highly appreciated!

A UIActionSheet has three distinct button display styles: A destructive button ( Red ) for.. well, destructive actions, a cancel button, which is the bottom one and slightly darker than the regular buttons, and the so-called "other buttons", which are color gray.
Now, if you want to go with a no-hacky-hacky solution, you can use those three colors to create the effect you need, by simply creating a if-condition and instantiating your actionsheet a bit differently every time.
If you are all into hacky-hacky solutions and want some more colors, you might want to consider iterating over all subviews in the actionsheet and modify their style directly. But be warned: your solution might not work in future releases of iOS since there is no guarantee that internal components without a public API won't change ( That's why there is no public API for it in most cases ).

Related

iOS7 Keyboard Behavior with SearchBar/UITableView: Offset on secondary view appearances

Problem
I am having a rather big issue with the iOS7 keyboard appearance. I have a Searchbar on a UIViewController with TableView Delegation/Data Source setup (I am using the self.searchDisplayController delegates as well). I segue from this scene to a prototype tableview to show the results.
Here is the issue:
On first load I can see the keyboard being displayed when I tap into the text field of the UISearchBar. I can type and perform a search with the results being shown in the next scene.
I've added NSNotifications to view the keyboard properties in local methods keyboardWillShow and keyboardWasShown. I can see on the first scene appearance (after the view is completely loaded):
I segue to the result tableview at this point and when I navigate back and touch the text field, my keyboard shows up either fully or partially off-screen:
When I look at the keyboardWillShow notification at this point I can see that my keyboard values are incorrect:
I've researched and tried many possibilities including:
Added the following to my main view controller:
-(BOOL)canResignFirstResponder
{
return YES;
}
-(BOOL)canBecomeFirstResponder
{
return YES;
}
Configured the following in my view did load
self.searchDisplayController.searchBar.spellCheckingType = UITextSpellCheckingTypeNo;
self.searchDisplayController.searchBar.autocapitalizationType= UITextAutocapitalizationTypeNone;
self.searchDisplayController.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchDisplayController.searchBar.keyboardType = UIKeyboardTypeDefault;
Put in standard stubs for:
-(void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
I've noticed that if I choose a Partial Curl as my segue mode, the keyboard remains accessible when I roll back to the main view controller (but then it was never fully off screen in that case). However if I move from the results tableview to a detail scene and then navigate back to the main view controller, the keyboard appears off-screen again.
Question
Is there a method I can use to intercept the misplaced keyboard so that it displays in the default location?
NB: Along these lines, I have created a NSDictionary property to hold the initial userInfo values with the correct keyboard placement. I am not sure how to reassign these values to get the keyboard to return to it's original placement.
BTW - This seems a bit of a hack to get the keyboard fixed due to a bug in IB, is there some other way that I can try to remedy the situation?
Thanks in advance for any feedback!
Solution
This was such an obscure issue that I'm sharing the solution to save the next person some effort. Like most programming issues, it turns out this one was self-inflicted. In my original iteration of this project I had turned off rotational support as I am learning auto-layout and I wanted to ease into the transition from Springs and Struts. Somehow between the start of the project and the code release I ended up with this bit of code in the Main Scenes' View Controller.
//BAD
- (NSUInteger) supportedInterfaceOrientations
{
return !UIInterfaceOrientationPortraitUpsideDown;
}
instead of returning a valid enumeration like...
//OK
- (NSUInteger) supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}

Scroll uitableview with slide uitableviewcell

I've done a custom UITableViewCell` with a front view and a back view for doing something like mailbox.
I've added the gestureRecognizer to scroll the front view so
self.slideRecognizer =
[[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(slide:)];
[self.slideRecognizer setDelegate:self];
[self addGestureRecognizer:self.slideRecognizer];
The problem is that I cant scroll the UITableView down unless I tap outside the UITableViewCell (for example on UITableView header) and go down.
There should be something to add or edit in my method
- (void)slide:(UIPanGestureRecognizer *)panRecognizer
It's true?
THAT'S THE SOLUTION FOR ME ____________________________________________________________________________
Based on Simon's link, I've added this code in my tableViewController and in my custom cell
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:
(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
The problem was that assigning the custom pan gestureRecognizer I've invalidating the default one.
I would take a look at this question which seems quite similar. It sounds as though the gesture is only being captured by that cell and not passed through.
Tell ScrollView to Scroll after other pan gesture
I'm not sure if this is exactly the same issue as yours however. You should post more code.
On a side note, personally as a developer and a user I find scrolling elements inside scrolling elements to be every irritating to use. From a usability perspective I would consider a redesign. Thats only my opinion however.

NSPopover - Hide when focus lost? (clicked outside of popover)

I'm using the doubleClickAction of a NSTableView to display a NSPopover. Something like this:
NSInteger selectedRow = [dataTableView clickedRow];
NSInteger selectedColumn = [dataTableView clickedColumn];
// If something was not selected, then we cannot display anything.
if(selectedRow < 0 || selectedColumn < 0)
{
NSLog(#"Invalid selected (%ld,%ld)", selectedRow, selectedColumn);
return;
} // End of something was not selected
// Setup our view controller, make sure if there was already a popover displayed, that we kill that one off first. Finally create and display our new popover.
DataInspectorViewController * controller =
[[DataInspectorViewController alloc] initWithNibName: #"DataInspectorViewController"
bundle: nil];
if(nil != dataPreviewPopover)
{
[dataPreviewPopover close];
} // End of popover was already visible
dataPreviewPopover = [[NSPopover alloc] init];
[dataPreviewPopover setContentSize:NSMakeSize(400.0f, 400.0f)];
[dataPreviewPopover setContentViewController:controller];
[dataPreviewPopover setAnimates:YES];
[dataPreviewPopover showRelativeToRect: [dataTableView frameOfCellAtColumn: selectedColumn row: selectedRow]
ofView: dataTableView
preferredEdge: NSMinYEdge];
Which works just fine. My popovers get created and removed on the cells that I double click on . The problem is, I want to the popover to go away if I click anywhere outside of it (like a single click on another cell). I have been looking around, but for the life of me cannot figure out how to do it.
This is something I would assume is built into a popover, (I'm fairly certain it was in the iOS UIPopoverController class) so I'm just wondering if im missing something simple.
You need to change the property behavior of your popover (in code or on interface builder) to:
popover.behavior = NSPopover.Behavior.transient;
NSPopover.Behavior.transient
The system will close the popover when the user interacts with a user interface element outside the popover.
Read more about this in Apple's documentation.
the .transient flag doesn't work for me.
However I can make things work by the following:
1) Whenever I show my popover I make sure I activate the app
(my app is a menu-bar app, so this doesn't happen automatically)
NSApp.activate(ignoringOtherApps: true)
2) When I click outside the app, then my app will be deactivated. I can detect this in the AppDelegate
func applicationWillResignActive(_ notification: Notification) {
print("resign active")
}
and act accordingly
After calling show(relativeTo:of:preferredEdge:) method,
Add below line
popover.contentViewController?.view.window?.makeKey()
And make sure you set
popover.behavior = .transient
Sorry, I've added solution in Swift.
While transient worked for most cases, it was an issue when the user interacted with elements outside of the application, as the popover would hide but not close.
What finally ended working for me was:
popover.behavior = .semitransient
Now the popover closes when changing app, or interacting with any other element outside of the app. But will not close when interacting with a NSMenu, and maybe won't close either with other interactions.
Quoting from the documentation for NSPopover.Behavior.semitransient:
The exact interactions that cause semi-transient popovers to close are not specified.
Similar to the documentation for NSPopover.Behavior.transient:
The exact interactions that will cause transient popovers to close are not specified.

Why is my UIActionSheet hidden by my TabBarController?

I am having a problem correctly implementing a UIActionSheet in an iPad 5.1 (XCode 4.3) project. I can populate it correctly with all the items I need. The list is longer than the window, but the scrollbars automatically come up, etc. However, the cancel button (which I presume is supposed to be at the end) is coming up half hidden behind my tab bar. Shown below:
(sorry, SO won't let me post images yet)
Here is my storyboard setup:
The entry point is that Tab Bar Controller on the left, which goes to another Navigation Controller (center), which has the View Controller on the right as the root view.
http://i854.photobucket.com/albums/ab103/srVincentVega/ScreenShot2012-06-28at52713PM.png
I have tried presenting the UIActionSheet in all sorts of ways, but this odd behavior persists, and I can't figure out how to address it
- (IBAction)cmdReason:(id)sender
{
NSArray *reasons = [AppState getInspReasons];
UIActionSheet *action = [[UIActionSheet alloc]
initWithTitle:#"Reason for Inspection"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:nil];
for (NSString *rsn in reasons)
{
[action addButtonWithTitle:rsn];
}
[action showInView:self.view];
}
I have tried the various methods to show "action" - showFromTabBar, showFromToolbar, etc - I am VERY new to this development environment, so I am not up to speed yet on how these items interact at this level. Does anyone have a suggestion for how I can present this correctly?
I am sorry if this has already been asked elsewhere. I have spent all day trying bits of code from all over the web, including SO. I don't know if it's something to do with my storyboard layout, or what.
One further thing - when I rotate the emulator, the action sheet does redraw, but the bit at the end there gets wonky looking, like it can no longer figure out how to draw it.
Many thanks!
EDIT:
I have put together a very small project that demonstrates this exact behavior. I don't have a good way to host the zip file, so I put on google docs and shared it. The link is below. If you click on that, there should be a download option under file that will give you the original zip file.
https://docs.google.com/open?id=0B7IYvy9_c_NLaEFneGc5bzc2S2c
Seems like there is not a real solution for this. It looks like it's a limitation with UIActionSheet if you add that amount of button titles and present that from a tab bar.
Beside that, the proper way to display an UIActionSheet from a tab bar is to use
[action showFromTabBar:self.tabBarController.tabBar];
instead of
// Taken from your example project
AppDelegate *d = [[UIApplication sharedApplication] delegate];
UIWindow *w = d.window;
UIViewController *vc = w.rootViewController;
UITabBarController *c = (UITabBarController *)vc;
UITabBar *t = c.tabBar;
[action showFromTabBar:t];
I would think if you got a reference to the tab bar controller then you should be able to present it from that. You can try showing it from the main window but I would think you shouldn't rely on that.
[action showInView:[[UIApplication sharedApplication] keyWindow]];
Try this:
CGRect r = CGRectMake(x, y, w, h); //change values to fit location of button
[actionSheet showFromRect:r inView:self.view animated:YES];
I used it on one of my apps with the same problem and the dismiss button showed up ok.

Validating fonts and colors NSToolbarItem items

Using Cocoa with latest SDK on OSX 10.6.6
I have an NSToolbar with custom toolbar items and also the built in fonts and colors NSToolbarItem items (NSToolbarShowFontsItem and NSToolbarShowColorsItem identifiers).
I need to be able to enable/disable those in various situations. Problem is validateToolbarItem: is never called for these items (it is being called for my other toolbar items).
The documentation is not very clear about this:
The toolbar automatically takes care
of darkening an image item when it is
clicked and fading it when it is
disabled. All your code has to do is
validate the item. If an image item
has a valid target/action pair, then
the toolbar will call
NSToolbarItemValidation’s
validateToolbarItem: on target if the
target implements it; otherwise the
item is enabled by default.
I don't explicitly set target/action for these two toolbar items, I want to use their default behavior. Does it mean I can't validate these items? Or is there any other way I can do this?
Thanks.
After some trial and error, I think I was able to figure this out and find a reasonable workaround. I will post a quick answer here for future reference for others facing the same problem.
This is just one more of Cocoa's design flaws. NSToolbar has a hardcoded behavior to set the target/action for NSToolbarShowFontsItem and NSToolbarShowColorsItem to NSApplication so as the documentation hints it will never invoke validateToolbarItem: for these NSToolbarItem items.
If you need those toolbar items validated, the trivial thing to do is not to use the default fonts/colors toolbar items but to roll your own, calling the same NSApplication actions (see below).
If using the default ones, it is possible to redirect the target/action of them to your object and then invoke the original actions
- (void) toolbarWillAddItem:(NSNotification *)notification {
NSToolbarItem *addedItem = [[notification userInfo] objectForKey: #"item"];
if([[addedItem itemIdentifier] isEqual: NSToolbarShowFontsItemIdentifier]) {
[addedItem setTarget:self];
[addedItem setAction:#selector(toolbarOpenFontPanel:)];
} else if ([[addedItem itemIdentifier] isEqual: NSToolbarShowColorsItemIdentifier]) {
[addedItem setTarget:self];
[addedItem setAction:#selector(toolbarOpenColorPanel:)];
}
}
Now validateToolbarItem: will be called:
- (BOOL)validateToolbarItem:(NSToolbarItem *)theItem {
//validate item here
}
And here are the actions that will be invoked:
-(IBAction)toolbarOpenFontPanel:(id)sender {
[NSApp orderFrontFontPanel:sender];
}
-(IBAction)toolbarOpenColorPanel:(id)sender {
[NSApp orderFrontColorPanel:sender];
}
I guess the engineers who designed this never thought one would want to validate fonts/colors toolbar items. Go figure.