StackCoder
StackCoder

Reputation: 81

UIScrollView going in infinite recursion

I want to implement a UIScrollView in my controller because the height of the view is greater than the iphone screen size.

I have my view designed in the story board for which I have created an IBOutlet in .h file

In .m, I am doing the following for creating UIScrollView:

-(void) addScrollView
{
    UIScrollView* ScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.ScrollView addSubview:self.myStoryBoardView];
    self.ScrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    [self.view addSubview:self.ScrollView];   
}

In the last line of the above, it goes in infinite loop. Kindly suggest

Upvotes: 0

Views: 213

Answers (3)

Salman Zaidi
Salman Zaidi

Reputation: 9842

Replace this line:

UIScrollView* ScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

With:

self.ScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

Explanation: You are initializing a separate local UIScrollView in your addScrollView method. You local object declaration overrides class instance's object declaration. However following code lines:

[self.ScrollView addSubview:self.myStoryBoardView];
self.ScrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:self.ScrollView];

are called on your class instance UIScrollView's object, which is uninitialized. That's why when you add uninitialized object as subview on any object, your app gets crashed.

Upvotes: 1

Manpreet Singh
Manpreet Singh

Reputation: 181

He is right. Either add scroll view in the storyboard or from the code. You are setting height of scroll view equal to the height of your self.view instead set height to self.myStoryBoardView.

 -(void) addScrollView
{
UIScrollView* scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scrollView addSubview:self.myStoryBoardView];
scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.myStoryBoardView.frame.size.height);
[self.view addSubview:scrollView];

}

Upvotes: 0

Michael
Michael

Reputation: 1107

You seem to make an UIScrollview (called ScrollView) yet you add self.ScrollView, which is probably a defined IBOutlet property (in your .h or .m file in the interface) made in Storyboard. Try this code and get rid of the UIScrollView in your Storyboard. Also don't use ScrollView but instead use scrollView as apple documentation suggests.

-(void) addScrollView
{
    UIScrollView* scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [scrollView addSubview:self.myStoryBoardView];
    scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    [self.view addSubview:scrollView];
}

Upvotes: 0

Related Questions