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 に答える