Norolim
Norolim

Reputation: 956

App crash when pressing a button in a subview

I've a big problem with a scrollView. I have this code and when i press a button that is in the xib file of "registerView" then the app crashes:

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" is added to a scrollView, but then when I push a button in "registerView" the app crashes with this log:

    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

Button: RegisterViewController.h:

- (IBAction)btn_reset:(id)sender;

RegisterViewController.m:

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

And I've tried to put a button in the scrollView instead but then i cant access to "textfields" info, so I need to put a button in the "RegisterView".

Upvotes: 1

Views: 649

Answers (3)

Kiran P Nair
Kiran P Nair

Reputation: 2021

Declare the RegisterViewController *registerView in @interface or create a @property for it.

@interface XXXXX ()
{
 RegisterView *registerView;
}

OR

@property (nonatomic, strong) RegisterView *registerView;

Upvotes: 4

Amal Jo
Amal Jo

Reputation: 43

In your case MainViewController is not retaining registerView object. For retaining the instance you need to add it as the childViewController of your MainViewController,

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

No need of declaring any additional properties just for that, UIViewController will retain all its childViewControllers.

Upvotes: 2

HG's
HG's

Reputation: 828

I'm guessing that its related to not retaining the "RegisterView" class. While you are adding its view to scrollView the class itself is released as soon as the end of the function is reached.

Try declaring your RegisterView class as a property as follows:

@property (nonatomic, strong) RegisterView *registerView;

And to add it in the scroll view:

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

Upvotes: 1

Related Questions