我正在尝试将我创建的按钮添加到数组中,然后从数组中删除它们。我的数组一直返回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 :(得分:2)
您忘了初始化wardArray
。你应该添加
wardArray = [NSMutableArray array];
到指定的初始化程序。
在Objective-C中向nil
个对象发送消息是合法的 - 这些消息被忽略了。这就是为什么你没有看到你添加的项目。
我还注意到你在视图中添加按钮,但是你永远不会删除它们。要从屏幕上删除按钮,请按如下所示更改代码:
- (IBAction)removeWard:(id)sender
{
[[self->wardArray lastObject] removeFromSuperview];
[self->wardArray removeLastObject];
}
答案 1 :(得分: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
}