2012-12-14 21 views
0

我已經建立了一個小項目,並在此我想調用另一個控制器中的方法。我已經像這樣設置了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 

在這裏你可以看到我的控制器的部分,但如果我運行的應用程序,它工作正常。然而,就在我點擊第二個按鈕時,它只顯示「Hello World!」而不是「你到達了終點區!」。我也嘗試過使用alertviews。有人可以教我如何解決這個問題嗎?我認爲在第一和第二之間沒有聯繫,但是爲什麼?

在此先感謝。

回答

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]; 

} 
+0

你的意思是在第二個? – MasterRazer

+0

在文件Second.m – yuf

+0

順便說一句,你在哪裏添加按鈕的視圖?你說這個按鈕可以工作,因爲你說你可以點擊它。但是從那個代碼中,按鈕甚至不會出現。 – yuf