按下子视图中的按钮时应用程序崩溃

时间:2014-05-12 09:31:25

标签: ios objective-c uiscrollview uibutton subview

scrollView我遇到了很大的问题。我有这个代码,当我按下“registerView”的xib文件中的按钮时,应用程序崩溃了:

MainViewController.m:

viewDidLoad中:

UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
scrollView.backgroundColor = [UIColor whiteColor];
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = NO;
scrollView.showsVerticalScrollIndicator = YES;
scrollView.showsHorizontalScrollIndicator = YES;
scrollView.contentSize = CGSizeMake(768, 1900);
[self.view addSubview:scrollView];


RegisterViewController *registerView = [[RegisterViewController alloc] init];
[scrollView addSubview:registerView.view];

“RegisterView”被添加到scrollView,但是当我按下“registerView”中的按钮时,应用程序崩溃了这个日志:

    2014-05-12 11:18:04.599 RegisterForm[16505:60b] -[UIScrollView modalPresentationStyle]: unrecognized selector sent to instance 0x13f517200
2014-05-12 11:18:04.602 RegisterForm[16505:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIScrollView modalPresentationStyle]: unrecognized selector sent to instance 0x13f517200'
*** First throw call stack:
(0x18a83709c 0x196c8dd78 0x18a83bd14 0x18a839a7c 0x18a7594ac 0x18d800a68 0x1000e76b4 0x18d70c2c0 0x18d70c044 0x18d7137dc 0x18d710ab0 0x18d78488c 0x1000e674c 0x18d7815cc 0x18d780fb8 0x18d77a9a0 0x18d70d530 0x18d70c720 0x18d77a0b0 0x190119128 0x190118c54 0x18a7f6fc8 0x18a7f6f28 0x18a7f514c 0x18a735b38 0x18d7792d4 0x18d7740e8 0x1000e7cd0 0x197277aa0)
libc++abi.dylib: terminating with uncaught exception of type NSException

按钮: RegisterViewController.h:

- (IBAction)btn_reset:(id)sender;

RegisterViewController.m:

    - (IBAction)btn_reset:(id)sender {
}

我试图在scrollView中放一个按钮,但后来我无法访问“textfields”信息,所以我需要在“RegisterView”中放一个按钮。

3 个答案:

答案 0 :(得分:4)

在@interface中声明RegisterViewController * registerView或为它创建一个@property。

@interface XXXXX ()
{
 RegisterView *registerView;
}

OR

@property(非原子,强)RegisterView * registerView;

答案 1 :(得分:2)

在您的情况下,MainViewController未保留registerView个对象。要保留该实例,您需要将其添加为childViewController的{​​{1}},

MainViewController

无需为此声明任何其他属性,RegisterViewController *registerView = [[RegisterViewController alloc] init]; [self addChildViewController:registerView]; [scrollView addSubview:registerView.view]; 将保留其所有UIViewController

答案 2 :(得分:1)

我猜它与不保留“RegisterView”类有关。当您将其视图添加到scrollView时,只要到达函数结束,就会释放类本身。

尝试将RegisterView类声明为属性,如下所示:

@property (nonatomic, strong) RegisterView *registerView;

并在滚动视图中添加它:

self.registerView = [[RegisterViewController alloc] init];
[scrollView addSubview:self.registerView.view];