2012-05-12 42 views
1

我想將我創建的按鈕添加到數組,然後從數組中刪除它們的按鈕。我的數組一直返回null,所以我感覺我的按鈕甚至沒有被添加到我的數組中?返回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 

回答

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  
} 
+0

優秀它現在增加了從陣列中刪除按鈕,但它不會從屏幕上刪除呢? –

+0

你只需要告訴他們從屏幕上刪除。我已經使用removeWard方法更新了我的答案,並向其中添加了一些代碼。我也改變了它的輕微變化,所以它不會僅僅刪除屏幕上的最後一個病房,而是你實際觸摸的那個病房。 –

2

你忘了初始化wardArray這隻會初始化一次。您應該添加

wardArray = [NSMutableArray array]; 

到您的指定初始化程序。

在Objective-C發送消息到nil對象是合法的 - 這些消息被忽略。這就是爲什麼你沒有看到你添加的項目。

我也注意到你在視圖中添加按鈕,但是你永遠不會刪除它們。若要從屏幕上刪除的按鈕,如下更改代碼:

- (IBAction)removeWard:(id)sender 
{ 
    [[self->wardArray lastObject] removeFromSuperview]; 
    [self->wardArray removeLastObject];  
}