在UISplitViewController中将一个int从MasterView传递给DetailView

时间:2012-03-28 23:16:26

标签: ios ipad uisplitviewcontroller

我有一个UISplitViewController,我试图将一个int值从MasterViewController传递给DetailTableViewController。主视图和详细视图控制器都是UITableViewControllers。我试图将indexPath.row值从master传递给detail,这将指示将哪个对象数组加载到detailViewController的tableview中。

MasterViewController.m中的一些代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int indexRowTouched = indexPath.row;

detailViewController = [self.splitViewController.viewControllers lastObject];
NSLog(@"%@", detailViewController);

detailViewController.detailItem = indexRowTouched;
NSLog(@"index row %i", indexRowTouched);

}

此处抛出异常

detailViewController.detailItem = indexRowTouched;

2012-03-28 16:08:19.625 splittest2 [14895:f803] - [UINavigationController setDetailItem:]:无法识别的选择器发送到实例0x6b592e0

来自DetailViewController.m

- (void)setDetailItem:(int)newDetailItem
{
if (_detailItem != newDetailItem) {
    _detailItem = newDetailItem;

    // Update the view.
    [self configureView];
    }

if (self.masterPopoverController != nil) {
    [self.masterPopoverController dismissPopoverAnimated:YES];
    }        
}

我还注意到,在选择了indexPath行之前,在加载detailView时,detailItem的值会立即记录为“0”。

我想知道为什么没有从MasterViewController分配这个属性,并且没有更新TableView。

修改

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int indexRowTouched = indexPath.row;

detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];

detailViewController.detailItem = indexRowTouched;
NSLog(@"index row %i", indexRowTouched);

}

尝试访问UINavigationController上的setDetailItem似乎已通过将didSelectRowAtIndexPath更改为上面的

来修复

1 个答案:

答案 0 :(得分:0)

- (void)configureView函数中,您正在执行

if (self.detailItem) {
    if (detailItem == 0) {
        NSString *medPath = [[NSBundle mainBundle] pathForResource:@"general" ofType:@"plist"];

        NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:medPath];

        self.items = dict;


        NSMutableArray *array = [[NSMutableArray alloc] init]; 

        [array addObjectsFromArray:[[self.items allKeys]
                                    sortedArrayUsingSelector:@selector(compare:)]];
        self.keys = array;

    }

    //etc
}

由于detailItemint,而不是对象,如果它等于0,if (detailItem)将返回false。因此,如果您向detailItem发送0,则不会输入您的configureView if语句,也不会发生任何事情。

你应该完全删除那个外部if语句并且只有:

- (void)configureView {
    if (detailItem == 0) {
        NSString *medPath = [[NSBundle mainBundle] pathForResource:@"general" ofType:@"plist"];

        NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:medPath];

        self.items = dict;


        NSMutableArray *array = [[NSMutableArray alloc] init]; 

        [array addObjectsFromArray:[[self.items allKeys]
                                    sortedArrayUsingSelector:@selector(compare:)]];
        self.keys = array;

    }

    //other cases for detailItem
}