Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.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 当我将按钮操作从后台加载到Mainclass时引发异常_Ios_Swift_Uiview - Fatal编程技术网

Ios 当我将按钮操作从后台加载到Mainclass时引发异常

Ios 当我将按钮操作从后台加载到Mainclass时引发异常,ios,swift,uiview,Ios,Swift,Uiview,我正在尝试将UIView从后台类加载到我的主类(这里我在UIView上添加了一个按钮)。正在加载ui视图,并且在该ui视图上添加了一个按钮,但当我点击它时,按钮操作正在抛出异常。我的代码如下: 背景类: 主要类别: 我正在从我的主类加载UIView,但是按钮操作引发了一个异常。我在这里做错了什么?您正在从另一个类调用一个方法,因此一旦您的视图完成加载,它就无法访问您添加为按钮目标的self change your method like this -(void)loadView1 :(UIVie

我正在尝试将
UIView
从后台类加载到我的主类(这里我在
UIView
上添加了一个按钮)。正在加载
ui视图
,并且在该
ui视图
上添加了一个按钮,但当我点击它时,按钮操作正在抛出异常。我的代码如下:

背景类: 主要类别:
我正在从我的主类加载
UIView
,但是按钮操作引发了一个异常。我在这里做错了什么?

您正在从另一个类调用一个方法,因此一旦您的视图完成加载,它就无法访问您添加为按钮目标的self

change your method like this
-(void)loadView1 :(UIView *)myview :(ViewController*)VC
{
    UIView * firstview =[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
    [firstview setBackgroundColor:[UIColor yellowColor]];
    UIButton *addProject=[[UIButton alloc]init];
    addProject = [UIButton buttonWithType: UIButtonTypeRoundedRect];
    addProject.frame = CGRectMake(100, 285, 100, 18);
    addProject.backgroundColor = [UIColor redColor];
    [addProject setTitle:@"Show View" forState:UIControlStateNormal];

    [addProject addTarget:VC action:@selector(ProjectPressed:) forControlEvents:UIControlEventTouchUpInside];
    [firstview addSubview:addProject];

    [myview addSubview:firstview];
}


and in main class
 call like this
[m1 loadView1:self.view :self];

also add you selector method to main class(ViewController.h)
- (void)ProjectPressed:(UIButton*)sender
{
    NSLog(@"ok");

}

您正在从另一个类调用一个方法,因此一旦您的视图完成加载,它就无法访问您添加为按钮目标的self

change your method like this
-(void)loadView1 :(UIView *)myview :(ViewController*)VC
{
    UIView * firstview =[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
    [firstview setBackgroundColor:[UIColor yellowColor]];
    UIButton *addProject=[[UIButton alloc]init];
    addProject = [UIButton buttonWithType: UIButtonTypeRoundedRect];
    addProject.frame = CGRectMake(100, 285, 100, 18);
    addProject.backgroundColor = [UIColor redColor];
    [addProject setTitle:@"Show View" forState:UIControlStateNormal];

    [addProject addTarget:VC action:@selector(ProjectPressed:) forControlEvents:UIControlEventTouchUpInside];
    [firstview addSubview:addProject];

    [myview addSubview:firstview];
}


and in main class
 call like this
[m1 loadView1:self.view :self];

also add you selector method to main class(ViewController.h)
- (void)ProjectPressed:(UIButton*)sender
{
    NSLog(@"ok");

}

谢谢,但在myview1类中,如果我们没有声明projectPrssed方法,它会显示类似未解析选择器(ProjectPressed)的警告,那么是否有任何解决方案来删除该警告Hi,请将其添加到您的ViewController.h文件-(void)ProjectPressed:(UIButton*)发送方;谢谢,但在myview1类中,如果我们没有声明projectPrssed方法,它会显示类似未解析选择器(ProjectPressed)的警告,那么是否有任何解决方案来删除该警告Hi,请将其添加到您的ViewController.h文件-(void)ProjectPressed:(UIButton*)发送方;什么是例外?什么是例外?
change your method like this
-(void)loadView1 :(UIView *)myview :(ViewController*)VC
{
    UIView * firstview =[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
    [firstview setBackgroundColor:[UIColor yellowColor]];
    UIButton *addProject=[[UIButton alloc]init];
    addProject = [UIButton buttonWithType: UIButtonTypeRoundedRect];
    addProject.frame = CGRectMake(100, 285, 100, 18);
    addProject.backgroundColor = [UIColor redColor];
    [addProject setTitle:@"Show View" forState:UIControlStateNormal];

    [addProject addTarget:VC action:@selector(ProjectPressed:) forControlEvents:UIControlEventTouchUpInside];
    [firstview addSubview:addProject];

    [myview addSubview:firstview];
}


and in main class
 call like this
[m1 loadView1:self.view :self];

also add you selector method to main class(ViewController.h)
- (void)ProjectPressed:(UIButton*)sender
{
    NSLog(@"ok");

}