gpt4 book ai didi

objective-c - 返回 null 的按钮数组

转载 作者:行者123 更新时间:2023-11-29 11:13:13 24 4
gpt4 key购买 nike

我正在尝试将我创建的按钮添加到数组中,然后从数组中删除它们。我的数组一直返回 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

最佳答案

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

wardArray = [NSMutableArray array];

到您指定的初始化程序。

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

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

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

关于objective-c - 返回 null 的按钮数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10560666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com