Blog Archives

Gotcha: UIActionSheet Cancel Button Error / Cancel Button Ignored

If you run into a problem that the Cancel button does not seem to work as expected inside a UIActionSheet, I feel your pain. Here’s what I figured out after a fair amount of trial and error (and frustration) – seems if you attempt to show a UIActionSheet when using a UITabBarController, you’ll run into display and event management problems.

For example, here is how you might traditionally allocate and request a UIActionSheet to be displayed:

// Create the action sheet
UIActionSheet *menu =
    [[UIActionSheet alloc] initWithTitle:
    @"Shopping List Options" delegate:self cancelButtonTitle:@"Cancel"
    destructiveButtonTitle:nil otherButtonTitles:@"Clear Shopping List",
    @"Sort by Recipe", @"Sort by Grocery Aisle", nil];
 
// Show in the current view
[menu showInView:self.view];

All the buttons above, with the exception of Cancel, work as expected. In my code, the Cancel button was essentially ignored, however, to make things interesting, every now and then the expected event (signifying dismissal of the event) was fired. Ugh.

Solution

There is a simple fix – if you change the view in which you want to show the actionsheet to point to the UITabBarController view, as shown here, things seems to by hunky-dory.

// Set the view to be the UITabBarController
[menu showInView:self.parentViewController.view];

This assumes of course that the parentViewController for the object in which you are creating the actionsheet is a UITabBarController.

Detect Single Tap in UIScrollView

If you’ve ever been using a UIScrollView and had a need to detect a single tap, I hear you. In the Savini Rims and Rides iPhone application, I had a horizontal scrollview of six cars as one element of the user interface. Selecting on a car (tapping once) was to launch a video. Trouble is, UIScrollView eats the single tap I was after.

Let me show you what I came up with to catch single taps within a UIScrollView. First, I created a subclass of UIScrollView as follows:

Subclass UIScrollView

@interface AppScrollView : UIScrollView
{
}
 
@end

The implementation of the AppScrollView is all of two methods, the initialization code and one method to manage touch events. The trick in detecting one touch is to look for the touchesEnded event, and within that method, check to see if the user is dragging when the event is triggered. If not dragging, pass the event up to the next responder – most likely, your class that contains an AppScrollView object.

AppScrollView Implementation

#import "AppScrollView.h"
 
@implementation AppScrollView
 
- (id)initWithFrame:(CGRect)frame
{
  return [super initWithFrame:frame];
}
 
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
{
  // If not dragging, send event to next responder
  if (!self.dragging)
    [self.nextResponder touchesEnded: touches withEvent:event];
  else
    [super touchesEnded: touches withEvent: event];
}
 
@end

From here we can create a new class that includes a AppScrollView object:

Class Interface Using AppScrollView

#import <UIKit/UIKit.h>
 
@class AppScrollView;
 
@interface SomeClass : UIViewController <UIScrollViewDelegate>
{
  AppScrollView *scrollView;
  ...
}
 
@end

Now, inside the implementation of the SomeClass class, all we need to do is look for the touchesEnded event that was passed up the responder chain. For example, a skeleton of the class using AppScrollView may look as follows:

Class Implementation Receiving Single Tap

#import "AppScrollView.h"
 
@implementation SomeClass
 
...
 
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
{
  // Process the single tap here
  ...
}
 
...
 
@end

As with everything else, it’s easy once you know the trick.

Using Application Badges

Several native applications on the iPhone use application badges as an indicator of new messages, think email and SMS. Creating badges is quite straightforward and is nothing more than a method call, passing in the desired number to display.

The image below shows how a badge may look when applied to your application. The code to [...]

WP Like Button Plugin by Free WordPress Templates