1

我有一个实现 refreshData 方法的 ViewController

在这个 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)

请帮助我该怎么做

4

1 回答 1

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];
于 2012-04-17T17:28:35.273 回答