0

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.

1 Answer 1

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]; } 
Sign up to request clarification or add additional context in comments.

2 Comments

you mean in the second one?
By the way, where do you add the button to the view? You said the button works since you said you can click it..but from that code the button shouldn't even show up

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.