在视图控制器之间保持值 [英] Keeping values between view controllers

查看:57
本文介绍了在视图控制器之间保持值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ItemAddViewController,它以模态视图的形式呈现.其中一个字段会推送一个新的视图控制器CategorySelectionViewController,它允许用户选择一个类别.

I have an ItemAddViewController, which presents itself as a modal view. One of the fields pushes a new view controller, CategorySelectionViewController, which allows the user to select a single category.

ItemAddViewController.h

ItemAddViewController.h

@property (nonatomic, retain) Category *category;

CategorySelectionViewController.h

CategorySelectionViewController.h

@property (nonatomic, retain) Category *category;

CategorySelectionViewController.m

CategorySelectionViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSManagedObject *currentCategory = category;

if (currentCategory != nil) {
    NSInteger   index = [categories indexOfObject:currentCategory];
    NSIndexPath *selectionIndexPath = [NSIndexPath indexPathForRow:index inSection:0];
    UITableViewCell *checkedCell = [tableView cellForRowAtIndexPath:selectionIndexPath];
    checkedCell.accessoryType = UITableViewCellAccessoryNone;
}

//set the checkmark accessory
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];

//update the category
category =[categories objectAtIndex:indexPath.row];
NSLog(@"%@", category);
// Deselect row
[tableView deselectRowAtIndexPath:indexPath animated:YES];

}

ItemAddViewController.m

ItemAddViewController.m

- (void)viewWillAppear:(BOOL)animated {
  NSLog(@"%@", category);
}

类别是在CategorySelectionViewController创建时设置的.在类别选择屏幕上选择类别后,NSLog将报告正确的对象.返回ItemAddViewController时,它再次为null.两者应该是同一个对象,所以我不确定自己在做什么错.

Category is set on CategorySelectionViewController creation. When category is selected on the category selection screen, NSLog reports the correct object. When it gets back to ItemAddViewController, it's null again. The two should be the same object, so I'm not sure what I'm doing wrong.

基本上,我需要一种在两个视图控制器之间传递数据的好方法.

Basically, I need a good method to pass data between two view controllers.

推荐答案

要跟进已讲的内容,在类似问题中通常采用的一种方法是使 ItemViewController (父代)成为委托 CategorySelectionViewController (子项),然后在 CategorySelectionViewController 中触发 tableView:didSelectRowAtIndexPath:时,向 ItemAddViewController中的委托回调发送消息-将所选类别作为参数传递.

To follow up on what's already been said, one approach commonly taken in similar problems is to make the ItemViewController (parent) the delegate of CategorySelectionViewController (child), and when tableView:didSelectRowAtIndexPath: fires in the CategorySelectionViewController, send a message to the delegate callback in ItemAddViewController - passing in the selected category as a parameter.

可以类似于以下内容来实现此概念:

This concept could be implemented similar to the following:

@protocol CategorySelectionViewControllerDelegate;

// in CategorySelectionViewController.h
@interface CategorySelectionViewController : UITableViewController {
    id<CategorySelectionViewControllerDelegate> delegate;
}
@property (nonatomic, assign) id<CategorySelectionViewControllerDelegate> delegate;
@end

@protocol CategorySelectionViewControllerDelegate
// delegate callback skeleton
-(void)userDidSelectCategory:(Category *)categorySelected;
@end

// in CategorySelectionViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   NSManagedObject *currentCategory = category;

    if (currentCategory != nil) {
        NSInteger   index = [categories indexOfObject:currentCategory];
        NSIndexPath *selectionIndexPath = [NSIndexPath indexPathForRow:index inSection:0];
        UITableViewCell *checkedCell = [tableView cellForRowAtIndexPath:selectionIndexPath];
        checkedCell.accessoryType = UITableViewCellAccessoryNone;
    }

    //set the checkmark accessory
    [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];

    // here's where you message the delegate callback
    [self.delegate userDidSelectCategory:[categories objectAtIndex:indexPath.row]];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

然后,将

ItemAddViewController的骨架修改为符合 CategorySelectionViewControllerDelegate 协议:

// in ItemAddViewController.h
@protocol CategorySelectionViewControllerDelegate;
@interface ItemAddViewController :  UITableViewController <CategorySelectionViewControllerDelegate> 
{ /* etc.... */ }
@property (nonatomic, retain) Category *category;
// delegate callback
-(void)userDidSelectCategory:(Category *)categorySelected

// in ItemAddViewController.m
// set the CategorySelectionViewController delegate as this ItemViewController when you instantiate it
-(void)showCategorySelectionViewController {
    CategorySelectionViewController *myChild = [[CategorySelectionViewController alloc] init];
    myChild.delegate = self;
    [self presentModalViewController:myChild animated:YES];
}
// implement the delegate callback
-(void)userDidSelectCateogry:(Category *)categorySelected {
    self.category = categorySelected;
    // other handling code as needed...
}

关于通过在 CategorySelectionViewController 中调用 [self parentViewController] 来执行此操作,要注意的是 ItemAddViewController 继承自 UITableView ,因此当您发送消息 [self parentViewController] 时,编译器会认为您正在与 UITableView 对话,而不是与 ItemAddViewController 对话,除非您明确将其强制转换.因此,它不知道 self.parentViewController 具有称为 category 的属性.您可以通过添加强制类型转换来解决此问题:

In regard to doing this by calling [self parentViewController] in CategorySelectionViewController, the catch is that ItemAddViewController inherits from UITableView, so when you send the message [self parentViewController], the compiler thinks you're talking to a UITableView, not an ItemAddViewController, unless you cast it explicitly. Therefore, it does not know that self.parentViewController has a property called category. You can fix this by adding the type cast:

ItemAddViewController *itemAddViewControllerParent = (ItemAddViewController *)[self parentViewController];
itemAddViewControllerParent.category = [categories objectAtIndex:indexPath.row]; 

希望这会有所帮助.

这篇关于在视图控制器之间保持值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆