简体   繁体   中英

Calling a Method from one class and used in another class

I have the a method in ViewController to draw a button. In ViewController2 I want to call the method and draw the button out.

In ViewController.h

@interface ViewController : UIViewController

-(void)method;
@end

ViewController.m

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

-(void)method{
    UIButton*Touch1= [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [Touch1 addTarget:self action:@selector(TouchButton1:) forControlEvents:UIControlEventTouchUpInside];
    [Touch1 setFrame:CGRectMake(50,50, 100, 100)];
    Touch1.translatesAutoresizingMaskIntoConstraints = YES;
    [Touch1 setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
    [Touch1 setExclusiveTouch:YES];
    [self.view addSubview:Touch1];

    NSLog(@"hi ");
}

-(void)TouchButton1:(UIButton*)sender{

    NSLog(@"hi again");

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Then I am trying to call from ViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    ViewController * ViewCon = [[ViewController alloc]init];
    [ViewCon method];
}

The NSLog shows correct text but no button was created. What is my problem?

Thanks

Create a separate class (called Utils , for example) with a class method that can be called from both view controllers:

@implementation Utils

+(void)methodForView:(UIView *)view
{
    UIButton*Touch1= [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [Touch1 addTarget:self action:@selector(TouchButton1:) forControlEvents:UIControlEventTouchUpInside];
    [Touch1 setFrame:CGRectMake(50,50, 100, 100)];
    Touch1.translatesAutoresizingMaskIntoConstraints = YES;
    [Touch1 setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
    [Touch1 setExclusiveTouch:YES];
    [view addSubview:Touch1];
}

@end

And call it like this:

- (void)viewDidLoad {
    [super viewDidLoad];
    [Utils methodForView:self.view];
}

Or better still, implement the method in a UIViewController -subclass and then derive all other view controllers from this base class.

i think this problem is caused by the scope of "self" on the method "method". Your button has been added into de firstView and not in the secondView. To do what you whant, you will have to pass the scope that you like to add the button. Like the sample given by @trojanfoe.

-(void)method:(UIView *)_view {
    UIButton*Touch1= [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [Touch1 addTarget:self action:@selector(TouchButton1:) forControlEvents:UIControlEventTouchUpInside];
    [Touch1 setFrame:CGRectMake(50,50, 100, 100)];
    Touch1.translatesAutoresizingMaskIntoConstraints = YES;
    [Touch1 setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
    [Touch1 setExclusiveTouch:YES];
    [_view addSubview:Touch1];

    NSLog(@"hi ");
}

And into your second view you can call:

 ViewController * ViewCon = [[ViewController alloc]init];
[ViewCon method:self.view];

i think thats the problem, i hope that will help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM