Ios 向主viewcontroller添加表,但内存泄漏?
以下是来自mainviewcontroller的代码Ios 向主viewcontroller添加表,但内存泄漏?,ios,objective-c,xcode,Ios,Objective C,Xcode,以下是来自mainviewcontroller的代码 #import "MainViewController.h" #import "ECSlidingViewController.h" #import "MenuViewController.h" @interface MainViewController () @end @implementation MainViewController { NSArray *toDoList; } @synthesize menuBtn;
#import "MainViewController.h"
#import "ECSlidingViewController.h"
#import "MenuViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController
{
NSArray *toDoList;
}
@synthesize menuBtn;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
// Do any additional setup after loading the view.
[super viewDidLoad];
//initialize table data
toDoList = [NSArray arrayWithObjects:@"Apples", "Bananas", "Soda", nil];
- (void)setTopViewController:(UIViewController *)theTopViewController
{
CGRect topViewFrame = _topViewController ? _topViewController.view.frame : self.view.bounds;
[self removeTopViewSnapshot];
[_topViewController.view removeFromSuperview];
[_topViewController willMoveToParentViewController:nil];
[_topViewController removeFromParentViewController];
_topViewController = theTopViewController;
[self addChildViewController:self.topViewController];
[self.topViewController didMoveToParentViewController:self];
[_topViewController.view setAutoresizingMask:self.autoResizeToFillScreen];
[_topViewController.view setFrame:topViewFrame];
_topViewController.view.layer.shadowOffset = CGSizeZero;
_topViewController.view.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.view.layer.bounds].CGPath;
[self.view addSubview:_topViewController.view];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [toDoList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"ToDoList";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [toDoList objectAtIndex:indexPath.row];
return cell;
}
从滑动视图控制器
#import "MainViewController.h"
#import "ECSlidingViewController.h"
#import "MenuViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController
{
NSArray *toDoList;
}
@synthesize menuBtn;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
// Do any additional setup after loading the view.
[super viewDidLoad];
//initialize table data
toDoList = [NSArray arrayWithObjects:@"Apples", "Bananas", "Soda", nil];
- (void)setTopViewController:(UIViewController *)theTopViewController
{
CGRect topViewFrame = _topViewController ? _topViewController.view.frame : self.view.bounds;
[self removeTopViewSnapshot];
[_topViewController.view removeFromSuperview];
[_topViewController willMoveToParentViewController:nil];
[_topViewController removeFromParentViewController];
_topViewController = theTopViewController;
[self addChildViewController:self.topViewController];
[self.topViewController didMoveToParentViewController:self];
[_topViewController.view setAutoresizingMask:self.autoResizeToFillScreen];
[_topViewController.view setFrame:topViewFrame];
_topViewController.view.layer.shadowOffset = CGSizeZero;
_topViewController.view.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.view.layer.bounds].CGPath;
[self.view addSubview:_topViewController.view];
}
initViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Main"];
}
和main.m
#import "AppDelegate.h"
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
崩溃是由
viewDidLoad
中的以下代码行引起的:
toDoList = [NSArray arrayWithObjects:@"Apples", "Bananas", "Soda", nil];
您正在尝试添加一个NSString
和两个C字符串。试试这个:
toDoList = [NSArray arrayWithObjects:@"Apples", @"Bananas", @"Soda", nil];
我很惊讶这行代码中没有编译器警告。永远不要忽略编译器警告。在我看来更像是崩溃。那个错误不是内存泄漏。该错误通常意味着您正在访问解除分配的对象。你应该为你的应用程序设置一个异常断点,看看它在哪里崩溃。@rdelmar是的,我上面粘贴的代码包括了异常发生的断点。你编写了TableView的所有必需方法吗?哦,哇。我花了5分钟才弄清楚你的代码和我的代码之间的区别。我要么喝咖啡,要么睡觉。谢谢