2012-09-22 104 views
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 馬里奧

回答

0

在你應用程序代理,當你設置myView.delegate =自MyView的實際上並沒有指向任何東西! 您需要設置MyView的指向您的ViewController。

使用這didFinishLaunchingWithOptions

myView = (ViewController *)self.window.rootViewController; 
myView.delegate = self; 
+0

在這裏,我們走了,現在它的工作原理。多謝。 – user595731