Here is a (really) basic wrapper for the UIAlertView in openFrameworks of iPhone. Not all of the necessary delegate methods have been implemented yet, but this is enough for me to get the proof of concept rolling in an app I’m building.
Feel free to let me know how I can do things better, or just tinker with the code directly at github: ofxiPhoneWrappers
Check out the example project. This is a bit of a kludge. I would like to figure out how to avoid having to declare main and testApp as mixed source files (.mm). It works for now, however.
Here are the notes on using the class:
// ofxiPhoneAlertView: // setup the list of buttons vector<string> otherButtons; otherButtons.push_back("Button 1"); otherButtons.push_back("Button 2"); // the args are: title, message, cancel button title ("" == omitted), vector of other button titles ofxiPhoneAlertView *alert = new ofxiPhoneAlertView("Title,"Here is the alert message.","",otherButtons); // display the alert view alert->show(); // I'm using the following methods in update() // test if the alert is visible (bool): alert->isVisible(); // test if the alert has been dismissed: alert->isDismissed(); // get the selected button (int): alert->getSelectedButton(); // if you use several alerts, you can distinguish between them via their title: alert->getTitle(); // example logic: switch(alert->getSelectedButton()){ case -1: // cancel, or no selection printf("alert = -1"); break; case 0: // first, non-cancel button printf("alert = 0"); break; case 1: // next button. will be the second button after Cancel printf("alert = 1, ok"); break; }
ofxiPhoneAlertView.h
/* ofxiPhoneAlertView.h Created on 6/10/09. Copyright 2009 Robert Carlsen | robertcarlsen.net This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ #import <UIKit/UIKit.h> #import "ofMain.h" @interface ofxiPhoneAlertViewDelegate : UIAlertView <UIAlertViewDelegate> { UIAlertView* _alertView; int selectedButton; bool dismissed; } - (id)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles; -(int)getButton; -(bool)isDismissed; -(string)getTitle; @end class ofxiPhoneAlertView { public: ofxiPhoneAlertView(string title, string message, string cancelButtonTitle, vector<string> otherButtonTitles); ~ofxiPhoneAlertView(); void show(); int getSelectedButton(); bool isVisible(); bool isDismissed(); string getTitle(); protected: ofxiPhoneAlertViewDelegate *alertViewDelegate; };
ofxiPhoneAlertView.mm
/* ofxiPhoneAlertView.mm Created on 6/10/09. Copyright 2009 Robert Carlsen | robertcarlsen.net This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include "ofxiPhoneAlertView.h" // c++ class methods ofxiPhoneAlertView::ofxiPhoneAlertView(string _title, string _message, string _cancelButtonTitle, vector<string> _otherButtonTitles){ NSString * title = [[[NSString alloc] initWithCString: _title.c_str()] autorelease]; NSString * message = [[[NSString alloc] initWithCString: _message.c_str()] autorelease]; // nil value will omit button NSString * cancelButtonTitle; if(_cancelButtonTitle == "") cancelButtonTitle = nil; else cancelButtonTitle = [[[NSString alloc] initWithCString: _cancelButtonTitle.c_str()] autorelease]; // the other buttons are an array of strings NSMutableArray *otherButtons = [NSMutableArray array]; for(int i=0;i<_otherButtonTitles.size();i++){ [otherButtons addObject:[[[NSString alloc] initWithCString: _otherButtonTitles[i].c_str()] autorelease]]; } alertViewDelegate = [[ofxiPhoneAlertViewDelegate alloc] initWithTitle:title message:message cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtons]; } ofxiPhoneAlertView::~ofxiPhoneAlertView(){ [alertViewDelegate release]; } void ofxiPhoneAlertView::show(){ [alertViewDelegate show]; } int ofxiPhoneAlertView::getSelectedButton(){ return [alertViewDelegate getButton]; } bool ofxiPhoneAlertView::isVisible(){ return [alertViewDelegate isVisible]; } bool ofxiPhoneAlertView::isDismissed(){ return [alertViewDelegate isDismissed]; } string ofxiPhoneAlertView::getTitle(){ return [alertViewDelegate getTitle]; } // obj-c implementation @implementation ofxiPhoneAlertViewDelegate - (id)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles { if (self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil ]){ _alertView = [[ofxiPhoneAlertViewDelegate alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil ]; for(NSString *s in otherButtonTitles){ [_alertView addButtonWithTitle:s]; [self addButtonWithTitle:s]; } dismissed = false; //self = [_alertView copy]; } return self; } -(int)getButton{ return selectedButton; } -(bool)isDismissed{ return dismissed; } -(string)getTitle{ // return the title of the alert. to differentiate between separate alert views return [[self title] UTF8String]; } /* - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated; { } */ // Called when a button is clicked. The view will be automatically dismissed after this call returns - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { selectedButton = buttonIndex; dismissed = true; NSLog(@"Selected button: %d",selectedButton); } /* // Called when we cancel a view (eg. the user clicks the Home button). This is not called when the user clicks the cancel button. // If not defined in the delegate, we simulate a click in the cancel button - (void)alertViewCancel:(UIAlertView *)alertView; - (void)willPresentAlertView:(UIAlertView *)alertView; // before animation and showing view - (void)didPresentAlertView:(UIAlertView *)alertView; // after animation - (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex; // before animation and hiding view - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex; // after animation */ -(void) dealloc{ [_alertView release]; [super dealloc]; } @end
Leave a Reply
You must be logged in to post a comment.