我正在尝试学习 ios 编码并尝试制作 foodDiary 应用程序。我使用了空项目文件,然后添加了故事板、视图控制器和表格视图控制器
当我运行我的程序时,tableView 菜单会显示其中的元素,然后当我按下 + 按钮时,会显示一个视图控制器,其中包含一个文本字段和一个按钮。当我输入一些内容并按下按钮时,我的程序会崩溃我得到断点错误,我在这里得到错误:
- (IBAction)addFoodButton:(id)sender {
NSString *newFood = [addFoodText text];
[foodTableViewController addFood:newFood];
[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];
}
我的部分编码是:
DAYfoodTableViewController.h
#import <UIKit/UIKit.h>
@interface DAYfoodTableViewController : UITableViewController
@property (nonatomic, strong) NSMutableArray *foodArray;
-(void) addFood:(NSString *) newFood;
@结尾
DAYfoodTableViewController.m
#import "DAYfoodTableViewController.h"
#import "DAYaddFoodViewController.h"
@interface DAYfoodTableViewController ()
@end
@implementation DAYfoodTableViewController
@synthesize foodArray;
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
DAYaddFoodViewController *addFoodViewController = [segue destinationViewController];
[addFoodViewController setFoodTableViewController:self];
}
-(void) addFood:(NSString *)newFood
{
[foodArray addObject:newFood];
[[self tableView] reloadData];
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
foodArray=[[NSMutableArray alloc] initWithObjects:@"Pizza",@"Chips",@"Sandwiches",@"Hot Dogs", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [foodArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"BasicCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
int rowNumber=[indexPath row];
NSString *food=[foodArray objectAtIndex:rowNumber];
[[cell textLabel] setText:food];
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[foodArray removeObjectAtIndex:[indexPath row] ];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade ];
/*
or (by asim)
if (editingStyle == UITableViewCellEditingStyleDelete) {
[foodArray removeObjectAtIndex:[indexPath row] ];
[[self tableView] reloadData];
}
*/
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
@end
DAYaddFoodTableViewController.h
#import <UIKit/UIKit.h>
#import "DAYfoodTableViewController.h"
@interface DAYaddFoodViewController : UIViewController
@property (nonatomic, weak) DAYfoodTableViewController *foodTableViewController;
- (IBAction)addFoodButton:(id)sender;
@property (weak, nonatomic) IBOutlet UITextField *addFoodText;
@end
DAYaddFoodTableView.m
#import "DAYaddFoodViewController.h"
@interface DAYaddFoodViewController ()
@end
@implementation DAYaddFoodViewController
@synthesize foodTableViewController;
@synthesize addFoodText;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)addFoodButton:(id)sender {
NSString *newFood = [addFoodText text];
[foodTableViewController addFood:newFood];
[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];
}
@end
奇怪的是,当程序崩溃并且我通过模拟器重新运行程序时,它可以完美运行.. 什么可能导致这个问题?谢谢