Ios 我刚刚将视图添加到scrollView,但我的应用程序崩溃了?

Ios 我刚刚将视图添加到scrollView,但我的应用程序崩溃了?,ios,uiscrollview,Ios,Uiscrollview,我有下面给出的.h文件 #import <UIKit/UIKit.h> @interface EliteScreen : UIViewController - (IBAction)homeBtnClk:(id)sender; - (IBAction)eliteIBtnClk:(id)sender; - (IBAction)eliteII_BtnClk:(id)sender; @end #import "EliteScreen.h" #import "EliteQuesti

我有下面给出的.h文件

#import <UIKit/UIKit.h>

@interface EliteScreen : UIViewController

- (IBAction)homeBtnClk:(id)sender;

- (IBAction)eliteIBtnClk:(id)sender;

- (IBAction)eliteII_BtnClk:(id)sender;

@end
#import "EliteScreen.h"
#import "EliteQuestionScreen.h"
#import "NextView.h"
#import "ScrollTestController.h"

@interface EliteScreen ()

@end

@implementation EliteScreen

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setfontForControll];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(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++) {

        ScrollTestController* optimztionscreen = [[ScrollTestController alloc] initWithNibName:@"ScrollTestController" bundle:[NSBundle mainBundle]];

        if (i == 0) {
            x = optimztionscreen.view.frame.origin.x;
        } else {
            x = optimztionscreen.view.frame.size.width + x;
        }
        NSLog(@"I %d",i);

        UIView *viewMine = optimztionscreen.view;
        [viewMine setBackgroundColor:[UIColor redColor]];
        viewMine.frame = CGRectMake(x, 0, optimztionscreen.view.frame.size.width,
                                    optimztionscreen.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];

    NSLog(@"MY VALUE FOR X %f",self.view.frame.size.width *viewcount);
}

- (IBAction)eliteII_BtnClk:(id)sender {
    [self addSubViewToScrollView];
}

- (IBAction)eliteIBtnClk:(id)sender {

}

@end
我的
ScrollTestController.m
文件如下

#import <UIKit/UIKit.h>

@interface ScrollTestController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@end
#import "ScrollTestController.h"
#import "AppConstant.h"
#import "SimpleTableCell.h"
#import "AssetPopUpController.h"

@interface ScrollTestController ()

@end

@implementation ScrollTestController {
    NSArray *tableData;
    NSArray *thumbnails;
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    tableData = [NSArray arrayWithObjects:first_screen_firstBtn_title,
                 first_screen_secondBtn_title,first_screen_thirdBtn_title, nil];

    thumbnails = [NSArray arrayWithObjects:@"assetImage_iPad.png",
                  @"assetImage_iPad.png",
                  @"assetImage_iPad.png",nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [tableData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
    static NSString *simpleTableIdentifier = @"SimpleTableCell";

    SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
    cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];

    [cell.nameLabel setFont:[UIFont fontWithName:@"UniversLTStd-BoldCn" size:13.00f]];
    [cell.nameLabel sizeToFit];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    SimpleTableCell *newCell = (SimpleTableCell *)[tableView cellForRowAtIndexPath:indexPath];

    NSString *Abc = newCell.nameLabel.text;
    NSLog(@"MY  TIME %@",Abc);

    AssetPopUpController* optimztionscreen = [[AssetPopUpController alloc] initWithNibName:@"AssetPopUpController" bundle:[NSBundle mainBundle]];
    [self.navigationController presentViewController:optimztionscreen animated:NO completion:nil];
    optimztionscreen.assetTitle.text = Abc;
    [optimztionscreen.assetTitle setFont:[UIFont fontWithName:@"UniversLTStd-BoldCn" size:20.00f]];
    [optimztionscreen.assetTitle sizeToFit];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 65;
}

@end
每当我单击EliteScreen中的eliteII按钮时,我的应用程序就会崩溃 错误是:

****BussinessAdvantage[7118:11303] *** -[ScrollTestController tableView:numberOfRowsInSection:]: message sent to deallocated instance 0x28532cb0****
我在xib文件中使用了表布局,并正确地设置了它的set-its-datasource和delagates方法


当我没有设置delagates和数据源时,它工作正常,但在设置数据源和delagates后,它会崩溃。

当您创建
EliteScreen
的实例时,您正在将其表视图添加到滚动视图中,但您没有保留
EliteScreen
的实例。因此,当表视图尝试加载数据时,会发生崩溃


保留EliteScreen的实例以解决问题。

为viewcontroller设置属性

@Property(nonatomic,retain)ScrollTestController* optimztionscreen;



self.optimztionscreen = 
         [[ScrollTestController alloc] initWithNibName:
         @"ScrollTestController" bundle:[NSBundle mainBundle]];

我刚试过你的代码,但还是会崩溃。并且给出了相同的错误。-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{return 1;//count of section}如果您遇到任何问题,请告诉我我已经输入了代码,在输入代码之后,它给出了错误[ScrollTestController numberOfSectionsInTableView:]:发送到解除分配实例0x752A340的消息使用断点并告诉要捕获异常的行-(NSInteger)numberOfSectionsInTableView:(UITableView*)表视图{return 1;//节计数}