【发布时间】:2013-04-24 05:26:14
【问题描述】:
我正在尝试找出代表,因为我确实需要他们来完成我正在从事的项目,但对于我的一生,我无法弄清楚他们。无论我如何调整代码,都没有任何效果
ViewController.h:
#import <UIKit/UIKit.h>
@class ViewController;
@protocol testDelegate
-(void)sayHi;
@end
@interface ViewController : UIViewController
- (IBAction)button:(id)sender;
@property (weak, nonatomic)id <testDelegate> delegate;
@end
ViewController.m:
#import "ViewController.h"
#import "DelegateController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
DelegateController *dc = [[DelegateController alloc] init];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)button:(id)sender {
[self.delegate sayHi];
}
@end
DelegateController.h:
#import "ViewController.h"
@interface DelegateController : UIViewController <testDelegate>
@end
DelegateController.m:
#import "DelegateController.h"
@interface DelegateController ()
@end
@implementation DelegateController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
NSLog(@"init");
ViewController *vc = [[ViewController alloc] init];
[vc setDelegate:self];
}
return self;
}
-(void)viewDidLoad
{
[super viewDidLoad];
}
-(void)sayHi
{
NSLog(@"hi");
}
@end
- (IBAction)button:(id)sender 方法是连接到一个按钮,但是当我点击时没有输出。我做错了什么?
【问题讨论】:
-
查看本教程enroyed.com/ios/…
标签: ios objective-c xcode delegates