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

Iphone 无法从委托调用控制器方法,iphone,objective-c,ios,xcode,Iphone,Objective C,Ios,Xcode,我正在尝试这样做: 到目前为止,我已经得到了这个结构:appDelegate->rootViewController->welcomeViewController 我的委托中有一个doSomething方法,它由welcomeViewController中的iAction调用。它可以工作,我可以在doSomething中执行NSlog,它显示该方法正在委托中被调用 问题是,当我在委托中的doSomething方法中运行类似于[rootViewController loadNewView]的命令时

我正在尝试这样做:

到目前为止,我已经得到了这个结构:appDelegate->rootViewController->welcomeViewController

我的委托中有一个doSomething方法,它由welcomeViewController中的iAction调用。它可以工作,我可以在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
RootViewController.h

@interface RootViewController : UIViewController {
    WelcomeViewController *welcomeView;
}

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

-(void)loadLocationList;
@interface WelcomeViewController : UIViewController

-(IBAction)viewList:(id)sender;
-(void)loadLocationList;
RootViewController.m

- (void)loadLocationList
{
    NSLog(@"RootViewController: loadLocationList");
}
- (void)viewList:(id)sender
{
    SurveyClientAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    [appDelegate doSomething];
}
欢迎光临

@interface RootViewController : UIViewController {
    WelcomeViewController *welcomeView;
}

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

-(void)loadLocationList;
@interface WelcomeViewController : UIViewController

-(IBAction)viewList:(id)sender;
-(void)loadLocationList;
WelcomeViewController.m

- (void)loadLocationList
{
    NSLog(@"RootViewController: loadLocationList");
}
- (void)viewList:(id)sender
{
    SurveyClientAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    [appDelegate doSomething];
}

您是否在welcomeViewController中保留对RootViewController的引用?rootViewController有可能在您可以调用它的方法之前被释放


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

当您调用doSomething时,rootViewController可能为零。你能检查一下吗?是的,这是怎么回事?我使用了rootViewController=[[rootViewController alloc]init];这很有效,谢谢你。发布一个答案,我会奖励你!