Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 返回null的按钮数组_Objective C_Ios - Fatal编程技术网

Objective c 返回null的按钮数组

Objective c 返回null的按钮数组,objective-c,ios,Objective C,Ios,我试图将我创建的按钮添加到一个数组中,然后将它们从数组中删除。我的数组一直返回null,所以我感觉我的按钮甚至没有被添加到数组中 我是初学者。我使用的是Xcode 4.3。这是我的密码: // // MainViewController.h // Test-Wards // // Created by Dayle Pearson on 5/12/12. // Copyright (c) 2012 __MyCompanyName__. All rights reserved. // #i

我试图将我创建的按钮添加到一个数组中,然后将它们从数组中删除。我的数组一直返回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
//
//MainViewController.h
//测试病房
//
//Dayle Pearson于2012年5月12日创作。
//版权所有(c)2012年\uuuu MyCompanyName\uuuuu。版权所有。
//
#导入“FlipsideViewController.h”
@接口MainViewController:UIViewController
{
/*这个东西创造了一个计时器*/
IBUILabel*对手蓝色;
n定时器*定时器;
int红蓝;
/*制作标签创建者的材料*/
CGPoint起点;
int xStuff,yStuff;
/*用于存储病房的阵列*/
NSMUTABLEARRY*wardArray;
}
@属性CGPoint startPoint;
-(iAction)startRedBlue:(id)发送方;
-(iAction)移除:(id)
发件人;
-(无效)倒计时;
@结束
//
//MainViewController.m
//测试病房
//
//Dayle Pearson于2012年5月12日创作。
//版权所有(c)2012年\uuuu MyCompanyName\uuuuu。版权所有。
//
#导入“MainViewController.h”
@接口MainViewController()
@结束
@实现主视图控制器
@合成起始点;
-(无效)倒计时
{
如果(红蓝<2){
[计时器失效];
定时器=零;
}
红蓝-=1;
opponentsBlue.text=[NSString stringWithFormat:@“%i”,红色蓝色];
}
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件
{
UITouch*触摸=[触摸任何对象];
startPoint=[触摸位置视图:self.view];
}
-(iAction)startRedBlue:(id)发送方
{
UIButton*wardButton=[UIButton Button类型:UIButtonTypeRoundRect];
wardButton.frame=CGRectMake((startPoint.x-5),(startPoint.y-5),10,10);
[wardButton设置标题:@“180”表示状态:UIControlStateNormal];
//增加目标和行动
/*[wardButton addTarget:自我操作:@selector()用于控制事件:*/
//添加到视图中
[self.view addSubview:wardButton];
[self->wardArray addObject:wardButton];
NSLog(@“This elemnt=%@”,wardArray);
}
-(iAction)removeWard:(id)发送方
{
[自->阵列移除对象];
}
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
}
-(无效)视图卸载
{
[超级视频下载];
//释放主视图的所有保留子视图。
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation
{
返回(interfaceOrientation!=UIInterfaceOrientation肖像向上向下);
}
#pragma标记-翻转侧视图
-(无效)FlipSideViewControllerdFinish:(FlipsideViewController*)控制器
{
[自我解散Modalviewcontrolleranimated:是];
}
-(void)prepareForSegue:(UIStoryboardSegue*)segue发送方:(id)发送方
{
if([[segue identifier]isEqualToString:@“showAlternate”]){
[[segue destinationViewController]setDelegate:self];
}
}
@结束

在添加到阵列之前,必须先初始化阵列

wardArray = [NSMutableArray array]; // quick and easy
wardArray = [[NSMutableArray alloc] init]; // oldschool
我建议在您的方法中这样做。如果它不存在,这只会初始化它一次,因此它不可能永远准备好拥有对象:)


您忘记初始化
wardArray
。您应该添加

wardArray = [NSMutableArray array];
到您指定的初始值设定项

在Objective-C中,向
nil
对象发送消息是合法的-这些消息被忽略。这就是为什么您看不到添加的项目

我还注意到,您向视图中添加了按钮,但从未将其删除。若要从屏幕中删除按钮,请按如下所示更改代码:

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

很好,它现在从数组中添加和删除按钮,但它不会从屏幕上删除按钮?您只需告诉他们从屏幕上删除按钮。我已使用removeWard方法更新了我的答案,并添加了一些代码。我还对其进行了轻微更改,这样它不仅会删除屏幕上的最后一个病房,而且会删除您实际要删除的病房青。
 - (IBAction)removeWard:(id)sender 
{
    [[self->wardArray lastObject] removeFromSuperview];
    [self->wardArray removeLastObject];    
}