我尝试一个简单的委托,但该方法不会触发。这是我的代码: 使用协议的视图和应该触发委托的按钮: 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 马里奥
答案 0 :(得分:0)
在你的App Delegate中,当你设置myView.delegate = self时,myView实际上并没有指向任何东西! 您需要将myView设置为指向ViewController。
在didFinishLaunchingWithOptions
myView = (ViewController *)self.window.rootViewController;
myView.delegate = self;