Objective c 从DetailViewController向MasterViewController推送和弹出信息

Objective c 从DetailViewController向MasterViewController推送和弹出信息,objective-c,xcode,uisplitviewcontroller,ipad,Objective C,Xcode,Uisplitviewcontroller,Ipad,我正在使用SplitViewController制作一个小型iPad应用程序,我主要使用DetailViewController上的MasterViewController。我正在尝试将一些数据从DetailViewController推送到MasterViewController。一旦信息被推送到MasterViewController,我想利用它,所以要做到这一点,我会使用pop方法 push方法填充数组-但由于某种原因,每次调用pushModuleTitle方法时都会重新创建数组,并且每次

我正在使用SplitViewController制作一个小型iPad应用程序,我主要使用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];);
}
 - (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);

    }
MasterViewController.m

- (IBAction)buttonAddPressed:(id)sender
{
    cw3MasterViewController *master = [[cw3MasterViewController alloc]init];
    [moduleTitles addObject:textFieldModuleTitle.text];
    [master pushModuleTitle:self.textFieldModuleTitle.text];);
}
 - (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]);

}

原因与你刚才问的另一个问题的答案相同。每次单击该按钮时,您都在创建cw3MasterViewController的新实例。您应该获得对主控制器的引用,如下所示:

cw3MasterViewController *master = self.splitViewController.viewControllers[0];
这假定master是拆分视图控制器索引0处的唯一控制器。如果它嵌入在导航控制器中(通常是这样),那么您需要进一步了解master:

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

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


改用委派制

我在哪里推广这一行呢?我是新来的this@user2382834,如果您希望在多个位置使用此引用,那么我将为master创建一个属性,并将该行放在详图视图控制器的viewDidLoad方法中。顺便说一句,只有在主控制器未嵌入导航控制器的情况下,此功能才能正常工作。是你的吗?如果,那么,我会更新我的答案。不确定,但我首先创建了一个主细节应用程序,所以所有的东西都是由xcode为我设置的