ofxiPhoneAlertView

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

Tags: , , , , , ,

4 Responses to “ofxiPhoneAlertView”

  1. stephan schulz Says:

    Hi.

    I am just getting into ofxiPhone but the example project that come with it really help.
    I really would like to get your code working but I am too much a beginner to figure how to make a proper project out of the posted code.
    maybe you can send me a zip file or post a link for the whole example project (including the *.xcodeproj files and directories).

    thank you for any help.

    cheers,
    stephan.

  2. Robert Says:

    Stephan:
    You can download all the files at the github link. Open the example project and drag ofxiPhoneAlertView.h and ofxiPhoneAlertView.mm into the files listing on the left of the Xcode window. Enable the “Copy files into project folder…” checkbox in the dialog sheet that will appear after you drag the files.

    The notes above indicate all the methods necessary to get the alert set up and to display. I would declare the alert in your testApp.h, then create a new alert using that variable in setup(). You can check whether on not the alert has been dismissed, and if it has check which button was pressed in update().

    I’ll see when I can get an example project posted.

  3. stephan schulz Says:

    Hi Robert.

    Yes an example project would help a lot.
    I followed you instructions (copy .h and .mm in my src folder through xcode) but I am getting 3009 errors.
    for example:
    conflicting declaration ‘typedef signed char int32_t’
    ‘NSString’ was not declared in this scope
    Foundation.framework/Headers/NSObjCRuntime.h:123: error: ‘aSelectorName’ was not declared in this scope

    i will wait for you example.

    thanks a lot,
    stephan.

  4. Robert Says:

    Stephan:

    That sounds like the UIKit framework isn’t getting included somehow. Perhaps I didn’t prepare something correctly when I tried to abstract out the Objective-C code. Try renaming main.cpp and testApp.cpp to main.mm and testApp.mm. Do it in the Finder, then drag then renamed files into Xcode again. The old .cpp files will be red in Xcode…remove them.

Leave a Reply