【问题标题】:addObject to NSMutableArray doesn't work with storyboardsaddObject 到 NSMutableArray 不适用于情节提要
【发布时间】:2013-12-10 22:28:49
【问题描述】:

我的根视图控制器中有一个表,其中“添加”按钮映射到另一个视图控制器,该控制器具有用于输入列表名称的文本字段和一个保存按钮。该保存按钮映射回主控制器。当数据传回主控制器时,它不会将项目添加到表中。

这是我的表视图控制器的 viewDidLoad 的样子:

 - (void)viewDidLoad
 {
     [super viewDidLoad];

     self.tableView.allowsMultipleSelectionDuringEditing = YES;

     if (!self.listArray) self.listArray = [[NSMutableArray alloc] init];
     [self.listArray addObject:@"New Item"];
     [self.tableView reloadData];

     [self updateButtonState];
  }

这会将“新项目”添加到列表中并显示在表格中。但是当我从 segue 传递列表名时,我可以看到日志条目“正在添加...”,但没有将项目添加到表中。这是从 segue 添加新项目的方法。

-(void) addList:(NSString *)listName
{
   NSLog(@"Adding %@", listName);
   [self.listArray addObject:listName];
   [self.tableView reloadData];

}

准备继续

  - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
     LSNewListViewController *sourceView = [segue sourceViewController];

     LSMainViewController *destView = [segue destinationViewController];
     [destView addList:[[sourceView listName] text]];
  }

【问题讨论】:

  • 你能发布你使用 addList: 操作的地方吗?
  • 我刚刚更新了....
  • 如果在viewDidLoadaddList: 方法中记录self 的值,您会看到什么? (我想知道是否有不止一个 LSMainViewController 正在以某种方式创建。)
  • 你在tableView:numberOfRowsInSection:方法中返回了什么?另外,尝试在主控制器viewDidAppear 方法中记录listArray
  • 我不确定这是否会有所帮助,但为什么不直接取出 LSMainViewController *destView = [segue destinationViewController];并做 [segue.destinationViewController addList:[[sourceView listName] text]];

标签: objective-c arrays storyboard nsmutablearray


【解决方案1】:

与其使用故事板 segue 来推送另一个视图控制器,不如尝试在“添加”视图控制器中设置协议。然后将您的“主”视图控制器设置为委托并在那里处理添加。从那里您可以连接“添加”按钮来调用委托方法并弹出视图控制器。

LSNewListViewController.h

@protocol LSNewListViewControllerDelegate;

@interface LSNewListViewController : UIViewController

@property (nonatomic, weak) id <LSNewListViewControllerDelegate> delegate;

@end

@protocol LSNewListViewControllerDelegate <NSObject>

@optional
- (void)didAddToList:(NSString*)item;

@end

LSNewListViewController.m

@implementation LSNewListViewController

// Hooked up to the Add button in the storyboard (touchUpInside)
- (IBAction)addToListAction:(id)sender {
    if ([self.delegate respondsToSelector:@selector(didAddToList:)]) {
        [self.delegate didAddToList:[sourceView listName] text]];
    }
}

@end

LSMainViewController.h

#import "LSNewListViewController.h"

@interface LSMainViewController : UIViewController <LSNewListViewControllerDelegate>

@end

LSMainViewController.m

@implementation LSMainViewController

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    LSNewListViewController *destView = [segue destinationViewController];
    destView.delegate = self;
}

// Delegate method
- (void)didAddToList:(NSString*)item {
    NSLog(@"Adding %@", listName);
   [self.listArray addObject:listName];
   [self.tableView reloadData];
   [self.navigationController popViewControllerAnimated:YES];
}

@end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    相关资源
    最近更新 更多