从子视图调用父视图方法

时间:2012-04-17 16:58:40

标签: objective-c

我有一个ViewController,它实现了一个refreshData方法

在这个ViewController中,我添加一个子视图,如下所示:

/* MainPageViewController.m
    //===============================================================================
    */

     #import "MainPageViewController.h"
     #import "SubViewController1.h"
     @implementation Drink4UMainPageViewController

    SubViewController1 *subViewController1;
    ......

     -(void)currentChatButtonMethod;
    {

        subViewController1 = [[SubViewController1 alloc]
                             initWithNibName:@"SubViewController1"
                             bundle:nil];


        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:subViewController1];
      [self.view addSubview:navController.view];
        // viewController.

        CGRect rect = navController.view.frame;
        CGPoint origin = CGPointMake(320, -10);
        rect.origin = origin;
        navController.view.frame = rect;


        //----------------------------------------------------------------------
        // Use a transform to slide it on.
        //----------------------------------------------------------------------
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.5];
        navController.view.transform = CGAffineTransformMakeTranslation(-320, -10);
        [UIView commitAnimations];

}

     -(void) refreshData
     {
     // CODE

    } 

从这里如何在“MainPageViewController”中调用“refreshData”方法?

    /*
    // SubViewController1.m
    =========================================================================================
    */


     #import "SubViewController1.h"
     #import <UIKit/UIKit.h>
     #import "MainPageViewController.h"

     @interface SubViewController1 : UIViewController <UITableViewDelegate, UITableViewDataSource, UINavigationControllerDelegate, UIAlertViewDelegate>
    {



    From here how can i call "refreshData" method in "MainPageViewController " ?



    }
    @end;

我正在从Mainviewcontroller添加一个子视图到UINavigationController现在我想从Subviewcontroller1中调用Mainviewcontroller中的方法(refreshData)

请帮助我该怎么做

1 个答案:

答案 0 :(得分:0)

您的子视图需要引用其父视图。为此,请在子视图控制器上创建一个属性,并在创建它时,让父视图控制器将对自身的引用传递给您创建的属性。

 @interface SubViewController1 : UIViewController <UITableViewDelegate, UITableViewDataSource, UINavigationControllerDelegate, UIAlertViewDelegate>
{    }


//Add this section <--------------------------------------
@property (nonatomic, retain) Drink4UMainPageViewController* parent;

-(void)DoStuff
{
    [parent refreshData];
}
@end;

-(void)currentChatButtonMethod;
{

    subViewController1 = [[SubViewController1 alloc]
                         initWithNibName:@"SubViewController1"
                         bundle:nil];

    subViewController1.parent = self;                     // Add this line<--------------


    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:subViewController1];
    [self.view addSubview:navController.view];