¿Delegar UIAlertView a otra clase/archivo? ¿No funciona?

so I'm new to iOS development and I'm trying to delegate the button click event to another class. Whenever I click a button on the alert, the app crashes and I get an error saying Thread_1 EXC_BAD_ACCESS.

Este es mi codigo.

// theDelegateTester.h
#import <UIKit/UIKit.h>

@interface theDelegateTester : UIResponder <UIAlertViewDelegate>
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
@end

Implementation..

// theDelegateTester.m
#import "theDelegateTester.h"

@implementation theDelegateTester
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSLog(@"Delegated");
}
@end

And here's the implementation for my view file..

#import "appleTutorialViewController.h"
#import "theDelegateTester.h"

@interface appleTutorialViewController ()
- (IBAction)tapReceived:(id)sender;
@end

@implementation appleTutorialViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}



- (IBAction)tapReceived:(id)sender {
    theDelegateTester *newTester = [[theDelegateTester alloc] init];
    UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Alert!" message:@"This is a delegated alert" delegate:newTester cancelButtonTitle:@"Close" otherButtonTitles:@"Cool!", nil];
    [myAlert show];
}
@end

preguntado el 26 de agosto de 12 a las 00:08

1 Respuestas

First of all, you should always start your class names with a capital letter, so you can differentiate between classes and instances or methods easily.

And you probably leak the delegate class. You should declare a strong/retained property TheDelegateTester *myDelegate in your view controller. Then in tapReceived: algo como esto:

- (IBAction)tapReceived:(id)sender {
    if (!self.myDelegate) {
        TheDelegateTester *del = [[TheDelegateTester alloc] init];
        self.myDelegate = del;
        [del release];
    }
    UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Alert!" message:@"This is a delegated alert" delegate:newTester cancelButtonTitle:@"Close" otherButtonTitles:@"Cool!", nil];
    [myAlert show];
    [myAlert release];
}

Respondido 26 ago 12, 00:08

Hmm, yeah that seems to do it, but why is it that I can't just create an object within the tapReceived and pass it in as the delegate parameter? Do I have to pass it to a property? Also, when I call release for del, or myAlert, also, my project uses Automatic Reference Counting (ARC) and does not let me release. As I understand, ARC is basically automatic garbage collection and release an object when it no longer sees a use for it? - braydon batungbacal

If you don't create a property, the instance of TheDelegateTester will simply run out of scope and be removed from memory. If you make it a property, that property will make sure it's not removed from memory. On your second question: yes. It's not entirely the same, but you don't have to (shouldn't) worry about retain/release with ARC. - Scott Berrevoets

The problem is, that delegates are usually weak (non-retained) references. This means you (the view controller) is responsible for keeping a reference to the TheDelegateTester instance. And that's exactly what you don't do. - BateristaB

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.