繁体   English   中英

无法从委托调用控制器方法

[英]Can't call controller method from delegate

我正在尝试做这样的事情: http : //www.pushplay.net/2009/05/framework-for-having-multiple-views-in-an-iphone-app/

到目前为止,我已经有了以下结构:appDelegate-> rootViewController-> welcomeViewController

我在委托中有一个方法(doSomething),该方法由IBAction在welcomeViewController中调用。 它可以工作,我可以在doSomething中执行NSlog,它表明该方法正在委托中被调用。

问题是,当我在doSomething方法(在委托中)中运行类似[rootViewController loadNewView]的命令时,它什么都不做。 它没有错误,它什么都不做。

我已经阅读并看到了建议的协议和通知,但是我想知道为什么使用委托的这种方法不起作用以及是否有任何方法可以解决它。

SurveyClientAppDelegate.h

@interface SurveyClientAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    RootViewController *rootViewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet RootViewController *rootViewController;

-(void)doSomething;

@end

SurveyClientAppDelegate.m

- (void)doSomething {
    NSLog(@"Attempt: rootViewController loadLocationList");
    [rootViewController loadLocationList];
}

RootViewController.h

@interface RootViewController : UIViewController {
    WelcomeViewController *welcomeView;
}

@property (nonatomic, retain) WelcomeViewController *welcomeView;
@property (nonatomic, retain) SurveyListViewController *surveyList;

-(void)loadLocationList;

RootViewController.m

- (void)loadLocationList
{
    NSLog(@"RootViewController: loadLocationList");
}

WelcomeViewController.h

@interface WelcomeViewController : UIViewController

-(IBAction)viewList:(id)sender;
-(void)loadLocationList;

WelcomeViewController.m

- (void)viewList:(id)sender
{
    SurveyClientAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    [appDelegate doSomething];
}

您是否在welcomeViewController中保留对rootViewContoller的引用? 在调用rootViewController上的方法之前,有可能(也可能会)释放rootViewController。

这是使用委托的好时机。 您提到了“在委托中”调用方法,但是在没有看到任何代码的情况下,很难判断您是否正确使用了该方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM