2

単純なデリゲートを試してみましたが、メソッドが起動しません。これが私のコードです:デリゲートをトリガーするプロトコルとボタンを含むビュー:ViewController.h

#import <UIKit/UIKit.h>
@protocol InitStackDelegate <NSObject>
@required
-(void)initstack:(NSInteger)amount;
@end

@interface ViewController : UIViewController{
    IBOutlet UITextField *amountTextField;
    __unsafe_unretained id<InitStackDelegate> delegate;
}

- (IBAction)init:(id)sender;

@property (nonatomic,assign)id  delegate;

@end

ViewController.m

#import "ViewController.h"


@interface ViewController ()

@end

@implementation ViewController
@synthesize delegate;
- (IBAction)init:(id)sender {
    //send the delegate

    [delegate initstack:[amountTextField.text intValue]];

}

AppDelegate.h

#import <UIKit/UIKit.h>
#import "ViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate,InitStackDelegate> {
}  

@property (strong, nonatomic) UIWindow *window;
@property (strong,nonatomic) ViewController *myView;
@end

AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window;
@synthesize myView;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    //..... push second controller into navigation stack
    myView.delegate = self;
    return YES;
}
...
#pragma mark Delegate Method
-(void)initstack:(NSInteger)amount{
    NSInteger test;
}

@end

ボタンを押すと、(IBAction) init が表示されますが、デリゲートは何もしません。AppDelegate.m にブレークポイントを設定しましたが、到達しません。何か助けはありますか?

thxマリオ

4

1 に答える 1

0

App Delegate で myView.delegate=self を設定すると、myView は実際には何も指しません! ViewController を指すように myView を設定する必要があります。

これをdidFinishLaunchingWithOptions

myView = (ViewController *)self.window.rootViewController;
myView.delegate = self;
于 2012-09-22T08:58:44.317 に答える