0

我正在尝试学习 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

奇怪的是,当程序崩溃并且我通过模拟器重新运行程序时,它可以完美运行.. 什么可能导致这个问题?谢谢

4

1 回答 1

0

从部分代码清单中可以看出,您没有完成所有必需的任务。您已将初始控制器设置为委托,但未将委托定义为头文件中的属性或在实现文件中合成它。此外,您没有将它添加到第二个控制器,而是试图调用您的第一个控制器的可选方法,并错误地调用它。我认为这就是它崩溃的原因。您使用过的序列,加上通知,可以完成委托可以完成的许多事情。您需要研究通知和序列......它允许您在控制器之间来回传递数据。而且,在您的情况下,您需要这样做。观看适当的 WWDC 视频,并完成一些教程,这些是最好的……http://www.raywenderlich.com/tutorials

于 2013-02-17T09:51:05.840 回答