1

我正在尝试将我创建的按钮添加到数组中,然后从数组中删除它们。我的数组一直返回 null 所以我觉得我的按钮甚至没有被添加到我的数组中?

我是初学者。我正在使用 Xcode 4.3。这是我的代码:

//
//  MainViewController.h
//  Test-Wards
//
//  Created by Dayle Pearson on 5/12/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "FlipsideViewController.h"

@interface MainViewController : UIViewController <FlipsideViewControllerDelegate> 
{
    /*This stuff creates a timer */
    IBOutlet UILabel *opponentsBlue;
    NSTimer *timer;
    int redBlue;

    /*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;

@end

//
//  MainViewController.m
//  Test-Wards
//
//  Created by Dayle Pearson on 5/12/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#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];

}


- (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];

    [self->wardArray addObject: wardButton];
    NSLog(@"This elemnt = %@", wardArray);


}
- (IBAction)removeWard:(id)sender 
{
    [self->wardArray removeLastObject];    
}
- (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

2 回答 2

2

你忘了初始化你的wardArray. 你应该添加

wardArray = [NSMutableArray array];

到您指定的初始化程序。

在 Objective-C 中向nil对象发送消息是合法的——这些消息被简单地忽略了。这就是您看不到您添加的项目的原因。

我还注意到您在视图中添加了按钮,但您从不删除它们。要从屏幕上删除按钮,请按如下方式更改代码:

 - (IBAction)removeWard:(id)sender 
{
    [[self->wardArray lastObject] removeFromSuperview];
    [self->wardArray removeLastObject];    
}
于 2012-05-12T02:54:59.617 回答
0

您必须先初始化数组,然后才能添加到它

wardArray = [NSMutableArray array]; // quick and easy
wardArray = [[NSMutableArray alloc] init]; // oldschool

我建议在你的方法中这样做。如果它不存在,这只会初始化一次,所以它不可能永远准备好拥有对象:)

- (IBAction)startRedBlue:(id)sender 
{
    ....

    // If wardArray doesn't exist we create it. Otherwise we add our ward to it.
    if (!wardArray) {
        wardArray = [NSMutableArray array];
    } else {
        [self->wardArray addObject: wardButton];
    }
}

- (IBAction)removeWard:(id)sender 
{
    UIButton *ward = (UIButton *)sender;
    [ward removeFromSuperview]; // Takes it off the screen.
    [self->wardArray removeObject:ward]; //Takes it out of the array    
}
于 2012-05-12T02:54:48.773 回答