我已经设置了一个小项目,在此我想在另一个控制器中调用一个方法。我已经在这个example中设置了。但是,我试图运行代码,但方法不会被调用!
有谁知道我做错了什么?
我的代码:
Second.h:
#import <UIKit/UIKit.h>
@class First;
@interface Second : UIViewController {
}
@property (nonatomic, retain) First* vC;
@end
Second.m:
#import "Second.h"
#import "First.h"
@interface First ()
@end
@implementation First
@synthesize vC;
- (IBAction)dothis:(id)sender {
NSLog(@"Hello World!");
[self.vC getThis];
}
-(void)viewDidLoad {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(dothis:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:nil forState:UIControlStateNormal];
button.frame = CGRectMake(5, 70, 270, 200);
[super viewDidLoad];
}
@end
First.h:
#import <UIKit/UIKit.h>
#import "Second.h"
@interface First: UIViewController {
//some Outlets and so on...
}
-(void)getThis;
@end
First.m:
#import "First.h"
@interface First ()
@end
@implementation First
-(void)getThis {
NSLog(@"You reached the end-zone!");
}
@end
在这里你可以看到我的控制器的部分,但如果我运行应用程序它工作正常。但是,当我单击Second上的按钮时,它只显示“Hello World!”而不是“你到达了终点区!”。我也尝试过使用alertviews。有人可以教我如何解决这个问题吗?我认为第一和第二之间没有联系,但为什么呢?
提前致谢。
答案 0 :(得分:1)
-(void)viewDidLoad {
self.vC = [[First alloc] init]; //need this
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(dothis:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:nil forState:UIControlStateNormal];
button.frame = CGRectMake(5, 70, 270, 200);
[super viewDidLoad];
}