xcode 无法在示例化为子视图的派生类中调用IBAction

zzlelutf  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(81)

因此,我尝试调用与从xib文件示例化的子视图的FileOwner关联的IBAction
我遇到了一个奇怪的问题,基本上我有一个所有视图都继承的基类:
ViewController.h

#import <UIKit/UIKit.h>
#import "CsoundManager.h"

@interface ViewController : UIViewController
-(IBAction) testActionBase:(id)sender;

@end

ViewController.m

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(IBAction) testActionBase:(id)sender
{
    NSLog(@"Test Action Fired");
}

@end

然后我还有另外两个视图类,它们继承了这个。一个是示例化子视图的主视图。
主视图文件:MainViewController.h

#import "ViewController.h"

@interface MainViewController : ViewController
-(IBAction) testFireDerived:(id)sender;
@end

MainViewController.m

#import "MainViewController.h"
#import "GlobalSequencerControlView.h"

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    GlobalSequencerControlView *globalControlView = [[GlobalSequencerControlView alloc] initWithNibName:@"GlobalSequencerControls" bundle:nil];
    //self.view.userInteractionEnabled = false;
    [self.view addSubview:globalControlView.view];
     
}

-(IBAction) testFireDerived:(id)sender
{
    NSLog(@"Main View Fire Derived");
}

@end

下面是示例化的子类文件:
GlobalSequencerControlView.h

#import "ViewController.h"

@interface GlobalSequencerControlView : ViewController {
    
}
-(IBAction) testFireDerived:(id)sender;
@end

GlobalSequencerControlView.m

#import "GlobalSequencerControlView.h"

@implementation GlobalSequencerControlView

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(IBAction) testFireDerived:(id)sender
{
    NSLog(@"Global Sequencer Fire Derived");
}
@end

当我在从xib加载的子视图中连接一个按钮时,我无法执行GlobalSequencerViewController.m中找到的testFireDerived IBAction。按钮接收触摸事件,因为我能够从继承的类中获取testActionBase方法,以便在链接到按钮时触发。
任何想法/想法,为什么这将是非常感谢。按钮似乎正在接收事件,但由于某种原因,派生的动作没有被触发,即使我以完全相同的方式将按钮连接到动作。

zengzsys

zengzsys1#

如果你想触发GlobalSequencerViewController的动作,那么添加“GlobalSequencerViewController”作为MainViewController的子视图控制器。而不是将其添加为MainViewController的子视图
两者的区别如下
addChildViewController将视图控制器与父容器视图控制器相关联,而addSubview将视图添加到它正在添加的视图的视图层次结构中。
因此,在添加子VC视图作为父视图的子视图后,在下面添加所述行

[self addChildViewController:globalControlView];

所以MainViewController的viewDidLoad代码如下

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view.

   GlobalSequencerControlView *globalControlView = [[GlobalSequencerControlView alloc] initWithNibName:@"GlobalSequencerControls" bundle:nil];
   //self.view.userInteractionEnabled = false;
   [self.view addSubview:globalControlView.view];
   [self addChildViewController:globalControlView];
}

相关问题