i try a simple Delegate but the method won’t fire. Here is my code:
The view with the protocol and a button which should trigger the delegate:
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
When i hit the button i get to (IBAction) init but then the delegate do nothing. I set a breakpoint in AppDelegate.m but it is never reached. Any help?
thx
Mario
In you App Delegate, when you set myView.delegate=self myView doesn’t actually point to anything!
You need to set myView to point to your ViewController.
Use this in
didFinishLaunchingWithOptions