iPhone 1 4-Mar-2010 4:43 PM
Edit FancyEdit New New Blog Upload All Recent Home Logout

Various bits and bobs

#define COOKBOOK_PURPLE_COLOR   [UIColor colorWithRed:0.20392f green:0.19607f blue:0.61176f alpha:1.0f]

#define BARBUTTON(TITLE, SELECTOR)  [[[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]
#define SYSBARBUTTON(ITEM, SELECTOR) [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:ITEM target:self action:SELECTOR] autorelease]
#define IMGBARBUTTON(IMAGE, SELECTOR) [[[UIBarButtonItem alloc] initWithImage:IMAGE style:UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]
#define CUSTOMBARBUTTON(VIEW) [[[UIBarButtonItem alloc] initWithCustomView:VIEW] autorelease]

#define ANIMATE(A_CHANGE) { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.3f]; {A_CHANGE;} [UIView commitAnimations]; }

#define STREQ(STRING1, STRING2) ([STRING1 caseInsensitiveCompare:STRING2] == NSOrderedSame)
#define PREFIXED(STRING1, STRING2) (<a href="/w2/?action=view&page=STRING1 uppercaseString] hasPrefix:[STRING2 uppercaseString">STRING1 uppercaseString] hasPrefix:[STRING2 uppercaseString</a>)
#define NUMBER(A_NUMBER) [NSNumber numberWithInt:A_NUMBER]

#define TRY_PERFORM(THE_OBJECT, THE_SELECTOR) (([THE_OBJECT respondsToSelector:THE_SELECTOR]) ? [THE_OBJECT performSelector:THE_SELECTOR] : nil)
#define TRY_PERFORM_WITH_ARG(THE_OBJECT, THE_SELECTOR, THE_ARG) (([THE_OBJECT respondsToSelector:THE_SELECTOR]) ? [THE_OBJECT performSelector:THE_SELECTOR withObject:THE_ARG] : nil)

#define IS_IPHONE           (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_OLDBAD    ([[UIScreen mainScreen] bounds].size.width < 768.0f)
#define IS_IPHONE_UNDOC     (TRY_PERFORM([UIDevice currentDevice], @selector(isWildcat))) // This is not kosher, boys and girls

#define IS_PORTRAIT UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])

#define NAMED_IMAGE(THE_NAME) \
([UIImage imageNamed:[NSString stringWithFormat:@"%@-%@-%@.png", THE_NAME, IS_IPHONE ? @"iphone" : @"ipad", IS_PORTRAIT ? @"portrait" : @"landscape"]] ?: \
([UIImage imageNamed:[NSString stringWithFormat:@"%@-%@.png", THE_NAME, IS_IPHONE ? @"iphone" : @"ipad"]] ?: \
([UIImage imageNamed:[NSString stringWithFormat:@"%@-%@.png", IS_PORTRAIT ? @"portrait" : @"landscape"]] ?: \
([UIImage imageNamed:[NSString stringWithFormat:@"%@.png", THE_NAME]]))))

#define BOOL_CHECK(TITLE, CHECK_ITEM)   printf("%s: %s\n", TITLE, (CHECK_ITEM) ? "Yes" : "No");

#define URLIMAGE(X) [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:X]]]
#define MAP_URL @"http://maps.weather.com/images/maps/current/curwx_720x486.jpg"

#define SEGMENT             [(UISegmentedControl *)self.navigationItem.titleView selectedSegmentIndex]

#define CFAutorelease(obj) ({CFTypeRef _obj = (obj); (_obj == NULL) ? NULL : [(id)CFMakeCollectable(_obj) autorelease]; })

#define SHOWRECT(SOME_CGRECT)   CFShow(NSStringFromCGRect(SOME_CGRECT))
#define SHOWPOINT(SOME_CGPOINT) CFShow(NSStringFromCGPoint(SOME_CGPOINT))

/* Animation quick ref
 [UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()];
 [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
 [UIView setAnimationDuration:0.5f];

 [UIView commitAnimations];
 */

// Return an exhaustive descent of the view's subviews
NSArray *allSubviews(UIView *aView)
{
    NSArray *results = [aView subviews];
    for (UIView *eachView in [aView subviews])
    {
        NSArray *riz = allSubviews(eachView);
        if (riz) results = [results arrayByAddingObjectsFromArray:riz];
    }
    return results;
}

// Return all views throughout the application
NSArray *allApplicationViews()
{
    NSArray *results = [[UIApplication sharedApplication] windows];
    for (UIWindow *window in [[UIApplication sharedApplication] windows])
    {
        NSArray *riz = allSubviews(window);
        if (riz) results = [results arrayByAddingObjectsFromArray: riz];
    }
    return results;
}

#pragma mark Show Alert Utility

void showAlert(id formatstring,...)
{
    NSString *outstring;
    va_list arglist;
    {
        if (!formatstring) return;
        va_start(arglist, formatstring);
        outstring = [[[NSString alloc] initWithFormat:formatstring arguments:arglist] autorelease];
    }
    va_end(arglist);

UIAlertView *av = [[[UIAlertView alloc] initWithTitle:nil [<a href="message:outstring">email</a>]delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
    [av show];
}


@implementation TestBedViewController
// Peekaboo
-(BOOL) respondsToSelector:(SEL)aSelector {
    printf("SELECTOR: %s\n", 
           [NSStringFromSelector(aSelector) UTF8String]);
    return [super respondsToSelector:aSelector];
}

- (void) trackNotifications: (NSNotification *) notification
{
    CFShow([notification name]);
    CFShow([notification object]);
    NSDictionary *dict = (NSDictionary *)[notification userInfo];
    for (id key in [dict allKeys])
        NSLog(@"  %@ : %@", key, [dict objectForKey:key]);
    // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(trackNotifications:) name:nil object:nil];
}