从DetailViewController到MasterViewController的推送和弹出信息

时间:2013-05-14 20:22:08

标签: objective-c xcode uisplitviewcontroller ipad

我正在使用SplitViewController制作一个小型iPad APP,我主要使用DetailViewController上的MasterViewController。我试图将一些数据从DetailViewController推送到MasterViewController。一旦信息被推送到MasterViewController,我想利用它,所以使用pop方法来做到这一点。

push方法填充数组 - 但由于某种原因,我每次调用pushModuleTitle方法时都会重新创建数组,并且它一次只能保存一个对象。

DetailViewController.m

- (IBAction)buttonAddPressed:(id)sender
{
    cw3MasterViewController *master = [[cw3MasterViewController alloc]init];
    [moduleTitles addObject:textFieldModuleTitle.text];
    [master pushModuleTitle:self.textFieldModuleTitle.text];);
}

MasterViewController.m

 - (NSMutableArray *)moduleTitleStack//init array
    {
        if (!_moduleTitleStack){
            _moduleTitleStack = [[NSMutableArray alloc] init];
        }
        return _moduleTitleStack;
    }

    -(void)pushModuleTitle:(NSString*)moduleTitile
    {
        NSString * moduleTitileObject = moduleTitile;
        [self.moduleTitleStack addObject:moduleTitileObject];
        NSLog(@"%@",self.moduleTitleStack);

    }

所以要使用推送的信息我正在使用这个pop方法: - 但是这总是返回一个空值,当我设置一个断点时它表示我的moduleTitleStack有0个对象。我不确定为什么。

-(NSString *)popModuleTitle
{
    NSString * moduleTitileObject = [self.moduleTitleStack lastObject];
    if (moduleTitileObject)[self.moduleTitleStack removeLastObject];
    return moduleTitileObject;
}

调用popModuleTitle方法:给出一个空值

- (IBAction)testButtonPressed:(id)sender {

    NSLog(@"%@", [self popModuleTitle]);

}

2 个答案:

答案 0 :(得分:0)

原因与您刚才提出的其他问题的答案相同。每次单击按钮时,您都在创建cw3MasterViewController的新实例。您应该像这样获得对主控制器的引用:

cw3MasterViewController *master = self.splitViewController.viewControllers[0];

这假定master是拆分视图控制器索引0处的唯一控制器。如果它嵌入在导航控制器中(通常是它),那么你需要更进一步才能掌握:

cw3MasterViewController *master = (cw3MasterViewController *)[(UINavigationController *) self.splitViewController.viewControllers[0] topViewController];

答案 1 :(得分:0)

ere:

w3MasterViewController * master =(cw3MasterViewController *)[(UINavigationController *)self.splitViewController.viewControllers [0] topViewController];

你有一个语义问题:下标需要接口'NSArray'的大小,这在非脆弱的ABI中不是常数

使用委派代替!