2
我嘗試一個簡單的委託,但該法將不會觸發。這是我的代碼: 與協議和一個按鈕的視圖,其應當觸發委託: ViewController.hIOS委託不火
#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爲)初始但隨後代表什麼也不做。我在AppDelegate.m中設置了一個斷點,但從未達到過。任何幫助?
THX 馬里奧
在這裏,我們走了,現在它的工作原理。多謝。 – user595731