MasterRazer
MasterRazer

Reputation: 1377

Call method in another class failure

I've set up a little project and in this I would like to call a method in another controller. I've already set up like in this example. However, I tried to run the code but the method doesnt gets called!

Does anyone know what I'm doing wrong?

My Code:

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

Here you can see the parts of my controllers but if I run the app it works fine. However, just when I click the button on Second it displays just "Hello World!" and not "you reached the end-zone!". I tried this with alertviews as well. Could someone teach me how to solve this? I think that there is no connection bewteen the First and the Second but why?

Thanks in advance.

Upvotes: 0

Views: 87

Answers (1)

yuf
yuf

Reputation: 3092

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

}

Upvotes: 1

Related Questions