Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从一个类调用方法并在另一个类中使用_Ios_Objective C_Class_Uibutton - Fatal编程技术网

Ios 从一个类调用方法并在另一个类中使用

Ios 从一个类调用方法并在另一个类中使用,ios,objective-c,class,uibutton,Ios,Objective C,Class,Uibutton,我在ViewController中有一个方法来绘制按钮。在ViewController2中,我想调用该方法并拉出按钮 在ViewController.h中 @interface ViewController : UIViewController -(void)method; @end ViewController.m @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } -(void

我在ViewController中有一个方法来绘制按钮。在ViewController2中,我想调用该方法并拉出按钮

在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
然后我试着从ViewController2打电话

- (void)viewDidLoad {
    [super viewDidLoad];
    ViewController * ViewCon = [[ViewController alloc]init];
    [ViewCon method];
}
NSLog显示正确的文本,但未创建任何按钮。 我有什么问题

谢谢

使用可以从两个视图控制器调用的类方法创建一个单独的类(例如称为
Utils
):

@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
这样称呼它:

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

或者更好的是,在
UIViewController
-子类中实现该方法,然后从该基类派生所有其他视图控制器。

我认为这个问题是由方法“method”上的“self”范围引起的。您的按钮已添加到de firstView中,而不是添加到secondView中。要做你想做的事情,你必须通过你想要添加按钮的范围。就像@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 ");
}
在第二个视图中,您可以调用:

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

我认为这就是问题所在,我希望这将帮助您添加完整的细节,以便我可以帮助您。这是一个糟糕的模式,因为它效率低下且不方便。创建一个具有您的功能的类,更易于重用。如果代码的另一部分使用相同的逻辑来实现该按钮,则可以使用“utils”类,而不使用可复制代码。但是,如果您的问题是一个在整个代码中只使用一次的特殊问题,那么使用分离类是解决简单问题的更复杂的方法。但是,像@trojanfoe这样的分类在他们答案的最后部分说是一个聪明的解决方案。