代表没有被召集

时间:2010-11-26 07:12:46

标签: iphone objective-c delegates ios4

我有一个viewcontroller“ResultsViewController”,带有一个名为emailbutton的按钮。当按下这个按钮时,我希望从另一个名为“Illusions_AppViewController”的视图中调用一个函数(这两个视图控制器都没有链接)。

因此,我在“ResultsViewController.h”中定义了一个协议:

 @protocol ResultsViewDelegate <NSObject>
 @optional 
 - (void) resultspage;

 @end

 @interface ResultsViewController : UIViewController
 {
     id<ResultsViewDelegate> mydelegate;
     UIButton *emailButton;
 }
 @property(nonatomic,retain) IBOutlet UIButton *emailButton;
 @property (nonatomic,assign) id<ResultsViewDelegate> mydelegate;
 @end

在ResultsViewController.m中:

-(IBAction)emailButtonPressed:(id)sender
{

    NSLog(@"entered emailbuttonpressed");// the app enters this method and gets hanged

    if ([mydelegate respondsToSelector:@selector(resultspage)]) {
        NSLog(@"entered respondstoselector");// this is never displayed in the log-showing that the delegates doesnt respond to selector
        [mydelegate resultspage];
    }

}

在我看来,“Illusions_AppViewController.m”:

- (void)resultspage{

    NSLog(@"Entered results page");
    ResultsPageController *resultspagecontroller = [[ResultsPageController alloc] initWithNibName:@"ResultsPageController" bundle:nil];

    resultspagecontroller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:resultspagecontroller animated:YES];
}   

如果有人能帮助我,我将不胜感激。我不知道为什么代表没有被调用。按下电子邮件按钮后,应用程序就会被挂起。谢谢!

3 个答案:

答案 0 :(得分:1)

代表的实施/使用是错误的。请参阅此tutorial

希望这有帮助。

谢谢,

Madhup

答案 1 :(得分:-1)

要回答第二个问题,您走在正确的轨道上。只需创建Illusions_AppViewController的一个实例,然后使用以下命令调用其中的illusionsView方法:

- (IBAction)emailButtonPressed {
   illusions_AppViewController *illusionsview = [[illusions_AppViewController alloc]init];
   [illusionsview resultspage];
   [illusionsview release];
}

答案 2 :(得分:-1)

还是有其他方法可以完成这项工作。我只需要在按下电子邮件按钮时调用结果页面功能。我试着用这种方式:

ResultsViewController.m

   -(IBAction)emailButtonPressed:(id)sender
    {

NSLog(@"entered emailbuttonpressed");

illusions_AppViewController *illusionsview = [[illusions_AppViewController alloc]init];
[illusionsview performSelector:@selector(resultspage)];
}

现在调用结果页面函数,但是它需要显示为modalviewcontroller的resultspagecontroller永远不会出现。应用程序挂起,控制台中也没有错误。

相关问题