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

视图控制器.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)初始化,但代表什么也不做。我在 AppDelegate.m 中设置了一个断点,但从未达到。有什么帮助吗?

谢谢马里奥

4

1 回答 1

0

在您的 App Delegate 中,当您设置 myView.delegate=self myView 实际上并没有指向任何东西!您需要将 myView 设置为指向您的 ViewController。

使用这个didFinishLaunchingWithOptions

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