Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 148 lines (117 sloc) 5.138 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
/*
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(){
// UI events need to occur on the main thread.
[alertViewDelegate performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];
}

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
Something went wrong with that request. Please try again.