一个视图中的多个控制器 [英] MultipleControllers in one view

查看:127
本文介绍了一个视图中的多个控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上是尝试将两个视图控制器添加到一个控制器。



我创建了一个名为MultipleViews的基于视图的应用程序。之后,我用自己的xib添加了两个控制器类RedView.h和BlueView.h。我可以通过方法 [self.view addSubview:red.view] 将两个控制器的视图添加到MutipleViewsViewController。两个视图都正确显示。问题是当我向红色和蓝色控制器添加一个按钮时。每当我点击按钮时,它会将无法识别的选择器发送到实例,即使我将按钮与其功能正确链接。我错过了什么吗?



这里是代码:



MultipleViewsViewController.h

  #import< UIKit / UIKit.h> 

@interface MutipleViewsViewController:UIViewController {

}

@end

MutipleViewsViewController.m



-

 (void)viewDidLoad {
[super viewDidLoad];

RedView * red = [[RedView alloc] init];

red.view.frame = CGRectMake(0,0,320,240);

[self.view addSubview:red.view];

BlueView * blue = [[BlueView alloc] init];

blue.view.frame = CGRectMake(0,240,320,240);

[self.view addSubview:blue.view];



}

RedView.h

  #import< UIKit / UIKit.h> 


@interface RedView:UIViewController {

}

- (IBAction)buttonPressed;

@end

BlueView.h

  #import< UIKit / UIKit.h> 


@interface BlueView:UIViewController {

}

- (IBAction)buttonPressed;

@end

按钮通过IB链接到buttonPressed方法。
当我点击红色视图中的按钮时,我得到的信息是:

  MutipleViews [1865:207] * **由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' -  [RedView buttonPressed]:无法识别的选择器发送到实例0x4e12500'

很抱歉早些时候不清楚。








解决方案

IBActions通常采用id类型的输入参数。所以你的 buttonPressed 动作应该看起来像

   - (IBAction)buttonPressed: (ID)发送者; 

实际调用此操作时,对控件的引用(在本例中为按钮)传递。



以编程方式调用它时,可以将控制器的对象( self )发送给它。 / p>

I am basically trying to add two view controllers to one controller.

I created a view based application called "MultipleViews". After that i add two controller classes "RedView.h" and "BlueView.h" with their own xibs. I am able to add the views of both the controllers to "MutipleViewsViewController" by the method [self.view addSubview:red.view]. Both the views are displayed properly. The problem is when I add a button to the red and blue controllers. Whenever I click the button it says unrecognized selector sent to instance even though I linked the buttons with their functions properly. Am i missing something here?

here is the code:

MultipleViewsViewController.h

#import <UIKit/UIKit.h>

@interface MutipleViewsViewController : UIViewController {

}

@end

MutipleViewsViewController.m

-

 (void)viewDidLoad {
    [super viewDidLoad];

    RedView *red = [[RedView alloc]init];

    red.view.frame = CGRectMake(0, 0, 320, 240);

    [self.view addSubview:red.view];

    BlueView *blue = [[BlueView alloc]init];

    blue.view.frame = CGRectMake(0, 240, 320, 240);

    [self.view addSubview:blue.view];



}

RedView.h

#import <UIKit/UIKit.h>


@interface RedView : UIViewController {

}

-(IBAction)buttonPressed;

@end

BlueView.h

#import <UIKit/UIKit.h>


@interface BlueView : UIViewController {

}

-(IBAction)buttonPressed;

@end

The buttons are linked to the buttonPressed method through IB. The message i get when i click the button in the red view is:

MutipleViews[1865:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RedView buttonPressed]: unrecognized selector sent to instance 0x4e12500'

Sorry for not being clear earlier.



解决方案

The IBActions typically take an input parameter of type id. So your buttonPressed action should look like

-(IBAction)buttonPressed:(id)sender;

When this action is actually called, a reference to the control which calls it (in this case the button) is passed.

When calling it programatically, you can send the controller's object (self) to it.

这篇关于一个视图中的多个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆