Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.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中获取UIButton以编程方式调用操作_Ios_Objective C_Uibutton - Fatal编程技术网

无法在iOS中获取UIButton以编程方式调用操作

无法在iOS中获取UIButton以编程方式调用操作,ios,objective-c,uibutton,Ios,Objective C,Uibutton,我无法使用按钮触发关联的方法。我不是在用IB或者类似的东西。_mainViewController是在自定义初始化期间传入的,只是为了澄清,尽管这与按钮无关。请参阅我的代码剪贴: viewcontroller.h 使用类型为init的按钮和显式userInteractionEnabled更新2016年9月10日 注12/9/16此处传递了_mainViewController对象: 在matt的帮助下,解决方案由于缺少添加新的视图控制器,上下文变得混乱 尝试: UIButton*theButto

我无法使用按钮触发关联的方法。我不是在用IB或者类似的东西。_mainViewController是在自定义初始化期间传入的,只是为了澄清,尽管这与按钮无关。请参阅我的代码剪贴:

viewcontroller.h

使用类型为init的按钮和显式userInteractionEnabled更新2016年9月10日

注12/9/16此处传递了_mainViewController对象:

在matt的帮助下,解决方案由于缺少添加新的视图控制器,上下文变得混乱

尝试:

UIButton*theButton=[UIButton Button类型:UIButtonTypeCustom]

然后设置你的框架

在自定义初始化期间传入_mainViewController

我想这就是问题所在-您对该视图控制器的管理不正确,并且它没有正确地成为视图控制器层次结构的一部分,事实上甚至可能不存在,因此没有人向其发送按钮操作消息

编辑是的,既然您发布了更多代码,我显然是对的:

CSNWZSSViewController *CSNWZSSVC = [[CSNWZSSViewController alloc] initWithRichTextString:source withTitleString:title forViewController:self.viewController];
[self.viewController.view addSubview:CSNWZSSVC.view];

这是完全错误的行为。您不能随意将另一个视图控制器的视图添加到您的视图中。管理子视图控制器有一个严格的过程,而您并没有这样做

你能试试吗:_doneButton.userInteractionEnabled=YES@Randy userInteractionEnabled在默认情况下为true,并且显式设置它没有影响。请参阅上面更新的代码10/9/16。@马特:是的,我可以看到按钮与其父视图headerView的区别。绿色显示出来,黑色或任何其他颜色的作品。尽管如此,doneButtonPressed方法从未被调用。请参阅上面更新的代码10/9/16。您的headerView的userInteractionEnabled是否也设置为YES?我真的不认为这能解决你的问题,但我们永远也不知道。你也可以试着把doneButton设置在头标上方。可能有一些奇怪的叠加。你能试着为headerLabel设置一个背景色,看看这个标签的确切位置吗?我本来有这个,但没有什么区别。我试了一下,但没有用。请参阅上面更新的代码10/9/16。这可能是真的。请参见上文12/9/16中关于如何传入mainViewController的注释。然而,_mainViewController在这个类中仅用于维度目的,尽管有点草率。我的情况到此为止。我知道这似乎是一种超越。你能教我跳这支舞吗?好极了!任何有兴趣学习舞蹈的人。这是一个很好的解释。
- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _mainViewController.view.frame.size.width, 45)];
    headerView.backgroundColor = [UIColor darkGrayColor];

    _doneButton = [[UIButton alloc] initWithFrame:CGRectMake(_mainViewController.view.frame.size.width - 100, 0, 100, headerView.frame.size.height)];
    [_doneButton addTarget:self
                   action:@selector(doneButtonPressed)
         forControlEvents:UIControlEventTouchUpInside];
    [_doneButton setTitle:@"Done" forState:UIControlStateNormal];
    _doneButton.tintColor = [UIColor whiteColor];
    _doneButton.backgroundColor = [UIColor colorWithRed:0 green:.77 blue:0 alpha:1];
    [headerView addSubview:_doneButton];

    CGRect headerLabelFrame = CGRectMake(5, 5, headerView.frame.size.width - 105, headerView.frame.size.height - 5);
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:headerLabelFrame];
    headerLabel.text = @"test header";
    headerLabel.textColor = [UIColor whiteColor];
    headerLabel.backgroundColor = [UIColor clearColor];
    headerLabel.font = [UIFont systemFontOfSize:20.0f];
    headerLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    [headerView addSubview:headerLabel];

    [self.view addSubview:headerView];
}

- (void)doneButtonPressed
{
    NSLog(@"button pressed");
}
_doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_doneButton addTarget:self
               action:@selector(doneButtonPressed)
     forControlEvents:UIControlEventTouchUpInside];
[_doneButton setTitle:@"Done" forState:UIControlStateNormal];
_doneButton.tintColor = [UIColor whiteColor];
_doneButton.backgroundColor = [UIColor blackColor]; //[UIColor colorWithRed:0 green:.77 blue:0 alpha:1];
_doneButton.frame = CGRectMake(_mainViewController.view.frame.size.width - 100, 0, 100, headerView.frame.size.height);
_doneButton.userInteractionEnabled = YES;
[headerView addSubview:_doneButton];
CSNWZSSViewController *CSNWZSSVC = [[CSNWZSSViewController alloc] initWithRichTextString:source withTitleString:title forViewController:self.viewController];
[self.viewController.view addSubview:CSNWZSSVC.view];
CSNWZSSViewController *CSNWZSSVC = [[CSNWZSSViewController alloc] initWithRichTextString:source withTitleString:title forViewController:self.viewController];
[self.viewController addChildViewController:CSNWZSSVC];
[self.viewController.view addSubview:CSNWZSSVC.view];
CSNWZSSViewController *CSNWZSSVC = [[CSNWZSSViewController alloc] initWithRichTextString:source withTitleString:title forViewController:self.viewController];
[self.viewController.view addSubview:CSNWZSSVC.view];