0

所以我的代码编译了,当我在 xcode 中运行我的 iphone 应用程序时,它崩溃并吐出大量内存错误,抱怨访问错误,我不知道这意味着什么。我要做的就是从我的超级视图中删除我创建的实验室在数组中使用计数器,结果比我想象的要困难得多。我对对象 c 和 iphone 开发非常陌生,任何帮助将不胜感激。

#import "FlipsideViewController.h"

@interface MainViewController : UIViewController <FlipsideViewControllerDelegate> 
{
    /*This stuff creates a timer */
    IBOutlet UILabel *opponentsBlue;
    NSTimer *timer;
    int redBlue;
    int count;
    /*Stuff for making a label creator */
    CGPoint startPoint;
    int xStuff, yStuff;

    /*array for storing wards*/
    NSMutableArray *wardArray;

}

@property CGPoint startPoint;

- (IBAction)startRedBlue:(id)sender;

- (IBAction)removeWard:(id)
sender;
- (void)countdown;
-(id)init;

@end

#import "MainViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController

@synthesize startPoint;

- (void)countdown 
{
    if (redBlue < 2) {

        [timer invalidate];
        timer = nil;
    }
    redBlue -= 1;
    opponentsBlue.text = [NSString stringWithFormat:@"%i", redBlue];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *theTouch = [touches anyObject];
    startPoint = [theTouch locationInView:self.view];

}

-(id)init{
    int count = 0;
    return self;
}

- (IBAction)startRedBlue:(id)sender 
{
    UIButton *wardButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    wardButton.frame = CGRectMake((startPoint.x - 5), (startPoint.y - 5), 10, 10);
    [wardButton setTitle:@"180" forState:UIControlStateNormal];
    //add targets and actions
    /*[wardButton addTarget:self action:@selector() forControlEvents:<#(UIControlEvents)#>*/
    //add to a view
    [self.view addSubview:wardButton];

    if (!wardArray) {
        wardArray = [NSMutableArray array];
    } 
    if (wardArray){
        [self->wardArray addObject: wardButton];
        count++;
    }

    NSLog(@"This elemnt = %@", wardArray);


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

    NSLog(@"The count is %@", count);
    [[wardArray objectAtIndex:count] removeFromSuperview];

    count--;

    NSLog(@"This elemnt = %@", wardArray);


}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

#pragma mark - Flipside View

- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
    [self dismissModalViewControllerAnimated:YES];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showAlternate"]) {
        [[segue destinationViewController] setDelegate:self];
    }
}

@end
4

1 回答 1

0

我猜测这不是 ARC(自动引用计数)。

wardArray 没有被保留,因此它在创建后不久就被释放。

您可以尝试替换[NSMutableArray array][[NSMutableArray alloc] init]. 这样它只会泄漏内存而不是留下一个僵尸。

此外,您可能想阅读有关 Objective-C 内存管理的内容。

于 2012-05-12T04:51:34.923 回答