I have a method which loads the view of a view controller as a subview to scrollview.The view of view controller which i tried to add as a subview contained a tableview in it.
-(void)addSubViewToScrollView{
CGFloat x = 0;
UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,
self.view.frame.size.width, self.view.frame.size.height)];
NSInteger viewcount= 8;
for(int i = 0; i< viewcount; i++) {
self.opt = [[EliteQuestionScreen alloc]
initWithNibName:@"EliteQuestionScreen"
bundle:[NSBundle mainBundle]];
if (i == 0) {
x = self.opt.view.frame.origin.x;
} else {
x = self.opt.view..frame.size.width + x;
}
UIView *viewMine = self.opt.view;
viewMine.frame = CGRectMake(x, 0, self.opt.view.frame.size.width,
self.opt.view.frame.size.height);
[scrollview setPagingEnabled:YES];
[scrollview addSubview:viewMine];
}
scrollview.contentSize = CGSizeMake(self.view.frame.size.width *viewcount,
self.view.frame.size.height);
[self.view addSubview:scrollview];
}
I have already reatined the object refrences of EliteQuestionScreen
like this
@property(strong,nonatomic)EliteQuestionScreen *opt;
but when this code exucute my app crashes and it is giving error
[EliteQuestionScreen tableView:numberOfRowsInSection:]: message sent to deallocated instance 0x716d6a0;
i don't able to undersatnd the behaviour?
But When i just closed the loop and add the view of a viewcontroller as a subview to scrollview it run well
-(void)addSubViewToScrollView{
CGFloat x = 0;
UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,
self.view.frame.size.width, self.view.frame.size.height)];
NSInteger viewcount= 8;
self.opt = [[EliteQuestionScreen alloc]
initWithNibName:@"EliteQuestionScreen"
bundle:[NSBundle mainBundle]];
x = self.opt.view.frame.origin.x;
UIView *viewMine = self.opt.view;
viewMine.frame = CGRectMake(x, 0, self.opt.view.frame.size.width,
self.opt.view.frame.size.height);
[scrollview setPagingEnabled:YES];
[scrollview addSubview:viewMine];
scrollview.contentSize = CGSizeMake(self.view.frame.size.width *viewcount,
self.view.frame.size.height);
[self.view addSubview:scrollview];
}
Why this is happening i am not able to figure out?
问题是控制器对象是本地的并且被释放了,您应该在.h文件中声明控制器对象
Try this,
for(int i = 0; i< viewcount; i++) {
EliteQuestionScreen *optScreen = [[EliteQuestionScreen alloc] initWithNibName:@"EliteQuestionScreen" bundle:[NSBundle mainBundle]];
if (i == 0) {
x = optScreen.view.frame.origin.x;
} else {
x = optScreen.view..frame.size.width + x;
}
//create and set a property in EliteQuestionScreen to identify different EliteQuestionScreen's (if needed)
UIView *viewMine = optScreen.view;
viewMine.frame = CGRectMake(x, 0, optScreen.view.frame.size.width,
optScreen.view.frame.size.height);
[scrollview setPagingEnabled:YES];
[scrollview addSubview:viewMine];
}
Update your -(void)addSubViewToScrollView
method as follows
-(void)addSubViewToScrollView {
NSInteger viewcount= 8;
// Code for adding subview inside scrollview
self.opt = [[EliteQuestionScreen alloc] initWithNibName:@"EliteQuestionScreen" bundle:[NSBundle mainBundle]];
[self addChildViewController: self.opt];
[scrollview addSubview:self.opt.view];
[self.opt didMoveToParentViewController: self];
scrollview.contentSize = CGSizeMake(self.view.frame.size.width *viewcount,
self.view.frame.size.height);
[self.view addSubview:scrollview];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.