0

だから私はiOS開発に不慣れで、ボタンクリックイベントを別のクラスに委任しようとしています。アラートのボタンをクリックするたびにアプリがクラッシュし、Thread_1EXC_BAD_ACCESSというエラーが表示されます。

これは私のコードです。

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

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

実装..

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

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

そして、これが私のビューファイルの実装です。

#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
4

1 に答える 1

1

まず第一に、クラスとインスタンスまたはメソッドを簡単に区別できるように、常にクラス名を大文字で始める必要があります。

そして、デリゲート クラスをリークする可能性があります。TheDelegateTester *myDelegateビュー コントローラーで、強力な/保持されたプロパティを宣言する必要があります。次に、tapReceived:次のようにします。

- (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];
}
于 2012-08-25T23:35:51.757 に答える